Adds one or more properties to an object, and/or modifies attributes of existing properties.
Syntaxobject.defineProperties( object , descriptors )
The object that was passed to the function.
ExamplesAdding Properties
In the following example, the Object.defineProperties function adds a data property and an accessor property to a user-defined object.
The example uses an object literal to create the descriptors object with the newDataProperty
and newAccessorProperty
descriptor objects.
var newLine = "<br />";
var obj = {};
Object.defineProperties(obj, {
newDataProperty: {
value: 101,
writable: true,
enumerable: true,
configurable: true
},
newAccessorProperty: {
set: function (x) {
document.write("in property set accessor" + newLine);
this.newaccpropvalue = x;
},
get: function () {
document.write("in property get accessor" + newLine);
return this.newaccpropvalue;
},
enumerable: true,
configurable: true
});
obj.newAccessorProperty = 10;
document.write ("newAccessorProperty value: " + obj.newAccessorProperty + newLine);
Like the previous example, the following example adds properties dynamically instead of with an object literal.
var newLine = "<br />";
var descriptors = new Object();
descriptors.newDataProperty = new Object();
descriptors.newDataProperty.value = 101;
descriptors.newDataProperty.writable = true;
descriptors.newDataProperty.enumerable = true;
descriptors.newDataProperty.configurable = true;
descriptors.newAccessorProperty = new Object();
descriptors.newAccessorProperty.set = function (x) {
document.write("in property set accessor" + newLine);
this.newaccpropvalue = x;
};
descriptors.newAccessorProperty.get = function () {
document.write("in property get accessor" + newLine);
return this.newaccpropvalue;
};
descriptors.newAccessorProperty.enumerable = true;
descriptors.newAccessorProperty.configurable = true;
var obj = new Object();
Object.defineProperties(obj, descriptors);
obj.newAccessorProperty = 10;
document.write ("newAccessorProperty value: " + obj.newAccessorProperty + newLine);
Modifying Properties
To modify property attributes for the object, add the following code. The Object.defineProperties function modifies the writable attribute of newDataProperty
, and modifies the enumerable attribute of newAccessorProperty
. It adds anotherDataProperty
to the object because that property name does not already exist.
Object.defineProperties(obj, {
newDataProperty: { writable: false },
newAccessorProperty: { enumerable: false },
anotherDataProperty: { value: "abc" }
});
Remarks
The descriptors argument is an object that contains one or more descriptor objects.
A data property is a property that can store and retrieve a value. A data property descriptor contains a value attribute, a writable attribute, or both. For more information, see Data Properties and Accessor Properties.
An accessor property calls a user-provided function every time the property value is set or retrieved. An accessor property descriptor contains a set attribute, a get attribute, or both.
If the object already has a property that has the specified name, the property attributes are modified. For more information, see Object.defineProperty Function.
To create an object and add properties to the new object, you can use the Object.create Function.
See also Other articlesMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4