(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

ParameterTypeMandatoryDescriptionUtil.js version
keyString(tick)

The name (key) of the property.

1.0
getterFunction(tick)The getter function to associate with the property.1.0
setterFunction(error)The setter function to associate with the property.1.0

Return value

ValueTypeNotesAPI Version
<object>ObjectIf 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
}
Availability STABLE

Requires:

Contents

Jump to:

Property Definitions

Topics: