Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Adds a new accessor (getter/setter) or data (value) property to any object along with setting it's enumerable, configurable and read-only flags as desired...

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
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
}

Where oObj is any object in the Javascript environment.

Parameters

ParameterTypeMandatoryDescriptionUtil.js version
nMaskNumber constant(tick)

A constant describing the property and its flags:

Child pages (Children Display)
alltrue
depthall
excerpttrue

0.1
sKeyString(tick)The name (key) of the property as a string.0.1
vVal_fGet

Value or

Function

(tick)

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

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

0.1
fSetFunction(question)If you're defining an accessor property (getter/setter) then this parameter is the setter function (or null if you're property is read-only).0.1

Return value

Returns whatever Javascript's defineProperty method returns, or throws an error.

Example

Code Block
themeRDark
languagejavascript
titleAdd .isAlly, .isEnemy and .isMe properties to player data objects
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);
    }
  );
}
Div
classbox

Availability

This feature requires:

  • Util.js v0.1 and above
  • Available on the global object/scope
Div
classbox

See also

Related articles:

  • defineProperty – I based uAddProperty on code examples from this MDN page
  • TypeError – this will be thrown if an existing non-configurable sKey property exists on the object
  • Function.uCurry() – create a new function that's based on an existing function but with some parameters pre-defined