Object.defineProperties Function (JavaScript)

Adds one or more properties to an object, and/or modifies attributes of existing properties.

Object.defineProperties(object, descriptors)

object

Required. The object on which to add or modify the properties. This can be a native JavaScript object or a DOM object.

descriptors

Required. A JavaScript object that contains one or more descriptor objects. Within the descriptors object, each descriptor object describes a data property or an accessor property.

The object that was passed to the function.

The descriptors argument is an object that contains one or more descriptor objects. Within the descriptors object, each descriptor object contains a property definition that describes the attributes of a data property or an accessor property.

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 Object.defineProperty Function (JavaScript).

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. For more information, see Object.defineProperty Function (JavaScript).

If the object already has a property that has the specified name, the property attributes are modified. For more information, see Object.defineProperty Function (JavaScript).

To create an object and add properties to the new object, you can use the Object.create Function (JavaScript).

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. This descriptors object contains descriptor objects that are named newDataProperty and newAccessorProperty. Within each descriptor object, the enumerable property, the configurable property, and other properties describe attributes of newDataProperty and newAccessorProperty.

function addProperties1() {
    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
        },
    });

    // Set the accessor property value.
    obj.newAccessorProperty = 10;
    document.write ("newAccessorProperty value: " + obj.newAccessorProperty + newLine);

    // Output:
    //  in property set accessor
    //  in property get accessor
    //  newAccessorProperty value: 10
}

Like the earlier example, the following example adds a data property and an accessor property to a user-defined object.

The properties of the descriptors object are created dynamically instead of by using an object literal. The descriptors object contains descriptor objects that are named newDataProperty and newAccessorProperty. Within each descriptor object, the enumerable property, the configurable property, and other properties describe attributes of newDataProperty and newAccessorProperty.

function addProperties2() {
    var newLine = "<br />";

    // Create the descriptors object.
    var descriptors = new Object();

    // Add a data property descriptor to the descriptors object.
    descriptors.newDataProperty = new Object();
    descriptors.newDataProperty.value = 101;
    descriptors.newDataProperty.writable = true;
    descriptors.newDataProperty.enumerable = true;
    descriptors.newDataProperty.configurable = true;

    // Add an accessor property descriptor to the descriptors object.
    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;

    // Call the Object.defineProperties function.
    var obj = {};
    Object.defineProperties(obj, descriptors);

    // Set the accessor property value.
    obj.newAccessorProperty = 10;
    document.write ("newAccessorProperty value: " + obj.newAccessorProperty + newLine);

    // Output:
    //  in property set accessor
    //  in property get accessor
    //  newAccessorProperty value: 10
}

To list each object property and its attributes, add the following code to the addProperties1 or addProperties2 function.

    // List the properties of the object.
    var names = Object.getOwnPropertyNames(obj);
    for (var i = 0; i < names.length; i++) {
        var prop = names[i];
        document.write(prop + newLine);

        // List the property attributes. Use a descriptor object that
        // is obtained with the Object.getOwnPropertyDescriptor function.
        var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
        for (var attr in descriptor) {
            document.write("..." + attr + ': ' + descriptor[attr]);
            document.write(newLine);
        }
        document.write(newLine);
    }

    // Output:
    //   newDataProperty
    //   ...writable: true
    //   ...value: 101
    //   ...configurable: true
    //   ...enumerable: true

    //   newAccessorProperty
    //   ...get: function () { document.write("in property get accessor" + newLine); return this.newaccpropvalue; }
    //   ...set: function (x) { document.write("in property set accessor" + newLine); this.newaccpropvalue = x; }
    //   ...configurable: true
    //   ...enumerable: true

    //   newaccpropvalue
    //   ...writable: true
    //   ...value: 10
    //   ...configurable: true
    //   ...enumerable: true

To modify property attributes for the object, add the following code to the addProperties1 function shown earlier.

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" }
    });

Supported in the Internet Explorer 9 standards document mode. See Version Information.

Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards.

Community Additions

Show:
© 2014 Microsoft