DEV Community

Cover image for -Object -getOwnPropertyDescriptor, -defineProperty
Husniddin6939
Husniddin6939

Posted on

-Object -getOwnPropertyDescriptor, -defineProperty

GetOwnPropertyDescriptor - Throught this key we can control our objects properties.

For example these keys controlled by users if it should be - Configurable, Enumerable, Writable.

Image description

let change={
    name:'Arda',
    theme:'Magic'

}

let result=Object.getOwnPropertyDescriptor(change, 'name');

console.log(result);
Enter fullscreen mode Exit fullscreen mode
                            ## Writable
Enter fullscreen mode Exit fullscreen mode

it is possable to type value again if we put true.

                            ## Enumerable
Enter fullscreen mode Exit fullscreen mode

Throught this value we can control loops - for in and for of works or not.

                            ## Configurable
Enter fullscreen mode Exit fullscreen mode

This key can remove and change object values.

                            ## DefineProperty
Enter fullscreen mode Exit fullscreen mode

This method also give pirmession or not to change object

let change={
    name:'Arda',
    theme:'Magic'

}


Object.defineProperty(change, 'theme', {
    value:"Netlify",
    enumerable:false,
    writable:false,
    configurable:false
});

change.smth='ok'

console.log(change);

for(let key in change){
    console.log('new',change[key]);
}
Enter fullscreen mode Exit fullscreen mode

Image description

in this case we change object theme to Netlify and use for in and show each property and finally we should say that if we have not to change we will control it from js.

Top comments (0)