object.addAccessor()

(info) This AI is still in early stages of development.

object.addAccessor()

Add a hidden, non-configurable accessor property to any object...

 

Syntax

obj.addAccessor(key, getter[, setter]);

Parameters

Parameter

Type

Mandatory

Description

Util.js version

key

String

:check_mark:

The name (key) of the property.

1.0

getter

Function

:check_mark:

The getter function to associate with the property.

1.0

setter

Function

:cross_mark:

The setter function to associate with the property.

1.0

Return value

Value

Type

Notes

API Version

<object>

Object

If successful, the addAccessor() 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

Adding a property to an object...
myObj = {};   var _foo = []; myObj.addAccessor( "foo", function getter() { return _foo; }, function setter(val) { _foo.push(val); } );   myObj.foo; // [] myObj.foo = true; myObj.foo; // [true]   myObj.foo = false; myObj.foo; // [true, false]   myObj.foo.push(null); myObj.foo; // [true, false, null]   for (var i in myObj) { // does not iterate foo (foo is hidden) }   for (var i in myObj.foo) { // 0, 1, 2 }