javascript Object getOwnPropertyDescriptors
getOwnPropertyDescriptors()
method in object finds complete information of property and returns property descriptors.
This will be useful for a real shallow copy of an object cloning, Before ES8, the Object will be copied using Object.assign() which is not suitable for complex object graphs.
- Syntax
Object.getOwnPropertyDescriptors(obj)
Parameters and return type
Input is an object
return property descriptors of a given input object
Example
const myobject = {
id:"1",
name:"Tom",
};
const descriptors = Object.getOwnPropertyDescriptors(myobject);
console.log(descriptors);
And the output is
{
id: {
value: '1',
writable: true,
enumerable: true,
configurable: true
},
name: {
value: 'Tom',
writable: true,
enumerable: true,
configurable: true
}
}
Descriptors
console.log(descriptors.id.writable); //true
console.log(descriptors.id.value); //1
console.log(descriptors.id.get); // undefined
console.log(descriptors.id.set); // undefined
console.log(descriptors.id.configurable); // true
console.log(descriptors.id.enumerable); // true
Descriptors allowed for each property are as follows.
- writable - the property allows to modify or not
- value - the value of a property key
- get - returns the getter function for the property if present, else undefined
- set - setter function of the property, if present, else undefined
- configurable - true, can be changed and will be deleted from an object, else false.
- enumerable - if true, will be treated as enumerable property and shown during iteration
Important points:
- It returns the own properties of an object
- It does not consider properties from `Object. prototype inheritance
Support This will support in latest browsers To support in older browsers, enable the following plugins in your project.
- polyfill
- babel plugin