Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Overview

Excerpt

Adds a new accessor (getter/setter) or data (value) property to any object

...

, setting it's enumerable, configurable and read-only flags as desired...

Availability

...

 

Syntax

Code Block

...

try {
  oObj.uAddProperty(nMask,sKey,vVal_fGet,fSet);
} catch(e) {
  // likely a TypeError if an existing non-configurable property of the same sKey is defined
}
themeRDark
languagejavascript
linenumberstrue
obj.addProp(mask, key, val_get[, set]);

Where obj is any object in the Javascript environment.

Parameters

ParameterTypeMandatoryDescriptionUtil.js version

...

maskNumber constant(tick)

A constant describing the property

...

descriptor settings:

Child pages (Children Display)
alltrue
depthall
excerpttrue

1.0

...

...

keyString(tick)The name (key) of the property as a string.1.0

...

...

val_

...

get

...

Variant or

Function

(tick)

If you're defining a data property, this parameter can be any Javascript value, function or object

...

.

If you're defining an accessor property (getter/setter) then this parameter is the getter function.

1.0

...

...

setFunction

...

(error)

If you're defining an accessor property (getter/setter) then this parameter is the setter function

...

.

If your accessor is read-only

...

, you don't need to specify a setter function.

1.0

...

Return value

...

ValueTypeNotesAPI Version
<object>ObjectIf successful, the addProp() method returns the modified object.1.0
<error>TypeError

An error will be thrown if:

  • The parameters are invalid, or
  • The object is not extensible, or
  • The key being modified is non-configurable.
1.0

Example

Code Block
theme

...

RDark
languagejavascript
titleAdd .isAlly, .isEnemy and .isMe properties to

...

Player Obejcts
linenumberstrue

...

playerData.primaryTarget = null;
for (var p=0; p<playerData.length; p++) {
  // Add a hidden and read-only .id property that returns the player number
  playerData[p].uAddProperty(
    DATA_READONLY_HIDDEN,
    "id",
    p
  );
  // Add a read-only .isAlly property that returns true if this player is an ally
  // Note: No setter function (fSet) means read only
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isAlly",
    allianceExistsBetween.uCurry(me,p)
  );
  // Add a read-only .isMe property that returns true if this player is me
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isMe",
    function() {
      return (this.id==me);
    }
  );
  // Add a read-only .isEnemy property that returns true if this player is an enemy
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isEnemy",
    function() {
      return (!(this.isAlly || this.isMe));
    }
  );
  // Add a read-write .isPrimaryTarget boolean property
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isPrimaryTarget",
    function() {
      return (playerData.primaryTarget == this.id);
    },
    function(bool) {
      playerData.primaryTarget = (!!bool) ? this.id : null;
      return (!!bool);
    }
  );
}

See also

...

// See https://warzone.atlassian.net/wiki/display/EGG/Function+Manipulation
// For info on curry() and wraps()
 
var isAlly = allianceExistsBetween.curry(me);
 
var NOT = funciton(bool) {
	return !bool;
}
 
var isEnemy = NOT.wraps(isAlly);

// Iterate the playerData array and add properties to objects
 
playerData.forEach(function(player, id) {
	// using .addAccessor() would be more concise
	playerData[id].addProp(
		ACCESSOR_HIDDEN,
		"isAlly",
		isAlly.curry(id)
	);
	playerData[id].addProp(
		ACCESSOR_HIDDEN,
		"isEnemy",
		isEnemy.curry(id)
	);
	// using .addConst() would be more concise
	playerData[id].addProp(
		DATA_HIDDEN_READONLY,
		"isMe",
		(id == me)
	);
});
 
playerData[somePlayer].isAlly; // true or false
// etc...
Div
classbox
Availability
Status
colourGreen
titleStable

Requires:

Div
classbox
Contents

Jump to:

Table of Contents
maxLevel5

Div
classbox

Property Definitions

Topics:

Child pages (Children Display)
alltrue
depthall
pageProperty Definitions
excerpttrue

Div
classbox

See also

Related articles:

  • defineProperty – Mozilla Developer Network page that inspired this .addProp() function