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/defineProperty below:

defineProperty · WebPlatform Docs

defineProperty Summary

Adds a property to an object, or modifies the attributes of an existing property.

Syntax
Object.defineProperty( object , propertyname , descriptor )
object
Required. The object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object.
propertyname
Required. A string that contains the property name.
descriptor
Required. A descriptor for the property. It can be for a data property or an accessor property.
Return Value

The modified object.

Examples

Adding a Data Property

In the following example, the Object.defineProperty function adds a data property to a user-defined object. To instead add the property to an existing DOM object, uncomment the var = window.document line.

var newLine = "<br />";


var obj = {};


Object.defineProperty(obj, "newDataProperty", {
    value: 101,
    writable: true,
    enumerable: true,
    configurable: true
});


obj.newDataProperty = 102;
document.write("Property value: " + obj.newDataProperty + newLine);






var names = Object.getOwnPropertyNames(obj);
for (var i = 0; i < names.length; i++) {
    var prop = names[i];

    document.write(prop + ': ' + obj[prop]);
    document.write(newLine);
}



Modifying a Data Property

To modify a property attribute for the object, add the following code to the addDataProperty function shown earlier. The descriptor parameter contains only a writable attribute. The other data property attributes remain the same.


Object.defineProperty(obj, "newDataProperty", { writable: false });



var descriptor = Object.getOwnPropertyDescriptor(obj, "newDataProperty");
for (var prop in descriptor) {
    document.write(prop + ': ' + descriptor[prop]);
    document.write(newLine);
}






Adding an Accessor Property

In the following example, the Object.defineProperty function adds an accessor property to a user-defined object.

var newLine = "<br />";


var obj = {};


Object.defineProperty(obj, "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 = 30;
document.write("Property value: " + obj.newAccessorProperty + newLine);








var names = Object.getOwnPropertyNames(obj);
for (var i = 0; i < names.length; i++) {
    var prop = names[i];

    document.write(prop + ': ' + obj[prop]);
    document.write(newLine);
}



Modifying an Accessor Property

To modify a property attribute for the object, add the following code to the code shown earlier. The descriptor parameter contains only a get accessor definition. The other property attributes remain the same.


Object.defineProperty(obj, "newAccessorProperty", {
    get: function () { return this.newaccpropvalue; }
});



var descriptor = Object.getOwnPropertyDescriptor(obj, "newAccessorProperty");
for (var prop in descriptor) {
    document.write(prop + ': ' + descriptor[prop]);
    document.write(newLine);
}






Modifying a Property on a DOM Element

The following example demonstrates how to customize built-in DOM properties by using the Object.getOwnPropertyDescriptor function to get and modify the property’s property descriptor. For this example, there must by a DIV element with an ID of "div".


var descriptor = Object.getOwnPropertyDescriptor(Element.prototype, "querySelector");


descriptor.value = "query";
descriptor.writable = false;

Object.defineProperty(Element.prototype, "querySelector", descriptor);


var elem = document.getElementById("div");


elem.querySelector = "anotherQuery";
document.write(elem.querySelector);



Remarks

You can use the Object.defineProperty function to do the following:

The property definition is provided in a descriptor object, which describes the attributes of a data property or an accessor property. The descriptor object is a parameter of the Object.defineProperty function.

To add multiple properties to an object, or to modify multiple existing properties, you can use the Object.defineProperties Function.

Exceptions

A TypeError exception is thrown if any one of the following conditions is true:

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