JavaScript - defineProperty() method

revision:


Category : object

The Object.defineProperty() static method defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

Syntax :

        Object.defineProperty(obj, prop, descriptor)
    

Parameters:

obj : required. The object on which to define the property.

prop : required. A string or Symbol specifying the key of the property to be defined or modified.

descriptor : required. The descriptor for the property being defined or modified.

Examples:

        const object1 = {};
        Object.defineProperty(object1, 'property1', {
            value: 42,  
            writable: false
        });
        object1.property1 = 77;
        // Throws an error in strict mode
        console.log(object1.property1);
        // Expected output: 42
    

Practical examples

example: creating a property

code:
                    <div>
                        <p id="define-1"></p>
                        <p id="define-2"></p>
                    
                    </div>
                    <script>
                        const person = { name: "John Doe" };
                        Object.defineProperty(person, "age", {
                            value: 30,
                            writable: false,
                            enumerable: true,
                            configurable: false,
                        });
            
                        console.log(person.age); // 30
                        document.getElementById("define-1").innerHTML = "age of person : " + person.age;
                        person.age = 35;
                        console.log(person.age); // 30
                        document.getElementById("define-2").innerHTML = "age of person : " + person.age;
                    </script>
                

example: JavaScript Object.defineProperty()

code:
                    <div>
                        <p id="define-3"></p>
                        <p id="define-4"></p>
                    </div>
                    <script>
                        // create an object named user
                        let user = {};
                        // define the name property of the user object 
                        Object.defineProperty(user, "name", {
                            value: "John",
                            writable: false
                        });
                        document.getElementById("define-3").innerHTML = "name of user : " + user.name;
                        // attempt to change the name property; change will fail silently
                        user.name = "John Doe";
                        console.log(user.name)
                        // Output: John
                        document.getElementById("define-4").innerHTML = "name of user : " + user.name;
                    </script>
                

example: defineProperty() method with Data Descriptors.

code:
                    <div>
                        <p id="define-5"></p>
                    
                    </div>
                    <script>
                        let obj = {};
                        // define object's property with data descriptors
                        Object.defineProperty(obj, "id", {
                            value: 711,
                            writable: true,
                            enumerable: true,
                            configurable: true,
                        });
            
                        console.log(obj.id); 
                        // Output: 711
                        document.getElementById("define-5").innerHTML = "id of object : " + obj.id;
                    </script>