Skip to content
Advertisements

Object.defineProperty()

Why Object.defineProperty ?

The Object.defineProperty () method allows for the precise addition or modification of a property in an object. By default, values ​​added using Object.defineProperty () do not change. The property identifiers found in objects come in two main ways: data identifiers and accessor identifiers. A data identifier is a property that can be written or written. The accessor identifier is a feature that is described by the receiver tuner function pairs. Both data and accessor identifiers are objects.

Syntax:

Object.defineProperty(obj, prop, descriptor)

obj – Property is defined on this object. 
prop – Name of the property 
descriptor – The descriptor for the property

configurable: Assigns “false” if no value is entered. true, and only if the type of this property identifier can be changed and the property can be deleted from the related object.
enumerable: Assigns “false” if no value is entered. If true, and only if this property occurs during enumeration of properties on the corresponding object.
value: Undefined by default. The value associated with the property. Any valid JavaScript value (number, object, function, etc.) can be.
writable: Assigns “false” true and only if the property-related value can be changed with an assignment operator.
get: Undefined by default. A function that functions as a recipient for the property or is undefined if a recipient is not present. When the property is accessed, this function is called the argument, and it is set to the object to which the property is accessed.
set: Undefined by default. A function that functions as a setter for the property or is undefined if there is no setting. When assigned to the property, this function is set to an argument and to the object to which the property is assigned.


JavaScript Object.defineProperty Examples

Example 1

        let myObject = {};
        Object.defineProperty(myObject, "website", {
            value: "truecodes.org",
            writable: false,
        });

        console.log(myObject.website)

output:

truecodes.org


Example 2

        let player = {
            level: 5
        };

        Object.defineProperty(player, "health", {
            get: function () {
                return 10 + (player.level * 15);
            }
        });

        console.log(player.health); 
        player.level++;
        console.log(player.health); 

output:

85
100


Examle 3

        let fullName = {
            name: "alex",
            surname: "allen"
        }

        Object.defineProperty(fullName, "getFullName", {
            value: function() {
                return this.name + " " + this.surname;
            }
        })

        console.log(fullName.getFullName())

output:

alex allen


Example 4

        var myObject = {};

        Object.defineProperty(myObject, "getValue", {
            value: 10,
            writable: true,
            enumerable: true,
            configurable: true
        });

        console.log("1.Property value: " + myObject.getValue);
        myObject.getValue = 20;
        console.log("2.Property value: " + myObject.getValue);

output:

1.Property value: 10
2.Property value: 20


Example 5

        let obj = {
            name: "alex"
        };

        let myObj = Object.create(obj);

        Object.defineProperty(myObj, "surname" , {
            value: "allen",
            writable: true
        });

        console.log("name: " + myObj.name);
        console.log("surname: " + myObj.surname);

output:

name: alex
surname: allen


Browser Support

Chrome5
Edgeyes
Firefox4
Internet Explorer9
Opera11.6
Safari5
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Android11.5
Advertisements

2 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: