A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webplatform.github.io/docs/javascript/Object/defineProperties below:

defineProperties ยท WebPlatform Docs

defineProperties Summary

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

Syntax
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. Each descriptor object describes a data property or an accessor property.
Return Value

The object that was passed to the function.

Examples

Adding 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 articles Attributions

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