JavaScript - getOwnPropertyDescriptor() method

revision:


Category : object

The Object.getOwnPropertyDescriptor() static method returns an object describing the configuration of a specific property on a given object (that is, one directly present on an object and not in the object's prototype chain). The object returned is mutable but mutating it has no effect on the original property's configuration..

Syntax :

        Object.getOwnPropertyDescriptor(obj, prop)
    

Parameters:

obj : required. The object in which to look for the property.

prop : required. The name or Symbol of the property whose description is to be retrieved.

Examples:

        const object1 = {
            property1: 42
        };
        const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
        console.log(descriptor1.configurable);
        // Expected output: true
        console.log(descriptor1.value);
        // Expected output: 42
          
    

Practical examples

example: using the Object.getOwnPropertyDescriptor() method.

code:
                    <div>
                        <p id="own-1"></p>
                        <p id="own-2"></p>
                        <p id="own-3"></p>
                        <p id="own-4"></p>
                    </div>
                    <script>
                    let o, d;
                    o = {
                        get foo() {
                            return 17;
                        },
                    };
                    d = Object.getOwnPropertyDescriptor(o, "foo");
                    console.log(d);
                    // {
                    //   configurable: true,
                    //   enumerable: true,
                    //   get: [Function: get foo],
                    //   set: undefined
                    // }
                    document.getElementById("own-1").innerHTML = "property descriptor :" + Object.entries(d);
                    o = { bar: 42 };
                    d1 = Object.getOwnPropertyDescriptor(o, "bar");
                    console.log(d1);
                    // {
                    //   configurable: true,
                    //   enumerable: true,
                    //   value: 42,
                    //   writable: true
                    // }
                    document.getElementById("own-2").innerHTML = "property descriptor :" + Object.entries(d1);
                    o = { [Symbol.for("baz")]: 73 };
                    d2 = Object.getOwnPropertyDescriptor(o, Symbol.for("baz"));
                    console.log(d2);
                    // {
                    //   configurable: true,
                    //   enumerable: true,
                    //   value: 73,
                    //   writable: true
                    // }
                    document.getElementById("own-3").innerHTML = "property descriptor :" + Object.entries(d2);
                    o = {};
                    Object.defineProperty(o, "qux", {
                        value: 8675309,
                        writable: false,
                        enumerable: false,
                    });
                    d3 = Object.getOwnPropertyDescriptor(o, "qux");
                    console.log(d3);
                    // {
                    //   value: 8675309,
                    //   writable: false,
                    //   enumerable: false,
                    //   configurable: false
                    // }
                    document.getElementById("own-4").innerHTML = "property descriptor :" + Object.entries(d3);
                    </script>
                

example: check if an object contains some property or not using the Object.getOwnPropertyDescriptor() method.

code:
                    <div>
                        <p id="own-5"></p>
                        <p id="own-6"></p>
                        <p id="own-7"></p>
                        <p id="own-8"></p>
                    </div>
                    <script>
                        let ninja, result;
                        ninja = { get foo() { return 22; } };
                        d5 = Object.getOwnPropertyDescriptor(ninja, 'foo');
                        console.log(d5);
                        document.getElementById("own-5").innerHTML = "property descriptor :" + Object.entries(d5);
                        ninja = { bar: 42 };
                        d6 = Object.getOwnPropertyDescriptor(ninja, 'bar');
                        console.log(d6);
                        document.getElementById("own-6").innerHTML = "property descriptor :" + Object.entries(d6);
                        ninja = { [Symbol.for('baz')]: 73 }
                        d7 = Object.getOwnPropertyDescriptor(ninja, Symbol.for('baz'));
                        console.log(d7);
                        document.getElementById("own-7").innerHTML = "property descriptor :" + Object.entries(d7);
                        ninja = {};
                        Object.defineProperty(ninja, 'qux', {
                            value: 8675309,
                            writable: false,
                            enumerable: false
                        });
                        d8 = Object.getOwnPropertyDescriptor(ninja, 'qux');
                        console.log(d8);
                        document.getElementById("own-8").innerHTML = "property descriptor :" + Object.entries(d8);
                    </script>