Object.getOwnPropertyDescriptors(obj)
accepts an object, and returns a new object that provides a list of the descriptors. Also, this method states all own (non-inherited) properties descriptors of an object. Stay continue with this tutorial and get complete details about JavaScript Object.getOwnPropertyDescriptors() Method, declaration or syntax, parameters, description, example using getOwnPropertyDescriptors().
- JavaScript Object.getOwnPropertyDescriptors() Method
- Syntax
- Parameter
- Return Value
- Browser Support
- Example using getOwnPropertyDescriptors()
- Other Example on getOwnPropertyDescriptors() in JavaScript
- Use cases for Object.getOwnPropertyDescriptors()
JavaScript Object.getOwnPropertyDescriptors() Method
The Object.getOwnPropertyDescriptors()
method returns all own property descriptors of a given object. The getOwnPropertyDescriptors() method ignores symbolic properties so this is the biggest difference between getOwnPropertyDescriptors()
and getOwnPropertyDescriptor()
method.
Do Check:
Syntax
The syntax of the JavaScript getOwnPropertyDescriptors()
method is:
Object.getOwnPropertyDescriptors(obj)
The getOwnPropertyDescriptors()
method, signifying a static method, is described applying the Object class name.
getOwnPropertyDescriptors() Parameters
ThegetOwnPropertyDescriptors()
method needs in:
obj
– It is the object for which to get all own property descriptors.
Return value from getOwnPropertyDescriptors()
JavaScript getOwnPropertyDescriptors()
method returns an object containing all own property descriptors of an object. Also, it may return an empty object in case there are no properties.
Browser Support:
Firefox | 50 |
Edge | 15 |
Chrome | 54 |
Opera | 41 |
Example using JavaScript getOwnPropertyDescriptors() Method
let obj = { x: 10, get number() { return this.x; }, }; let value = Object.getOwnPropertyDescriptors(obj); console.log(value); // getOwnPropertyDescriptors() can be used for shallow clone let cloneObj = Object.create( Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj) ); console.log(cloneObj); // { x: 10, number: [Getter] }
Output:
{ x: { value: 10, writable: true, enumerable: true, configurable: true }, number: { get: [Function: get number], set: undefined, enumerable: true, configurable: true } } { x: 10, number: [Getter] }
Other Example on getOwnPropertyDescriptors() in JavaScript
const dog = {} Object.defineProperties(dog, { breed: { value: 'Siberian Husky' } }) Object.getOwnPropertyDescriptors(dog) /* { breed: { value: 'Siberian Husky', writable: false, enumerable: false, configurable: false } } */
Use Case for Object.getOwnPropertyDescriptors() – Copying Properties into an object
There is one use case that makes this property very useful. ES2015 gave us Object.assign()
, which copies all enumerable own properties from one or more objects, and returns a new object. However, there is a problem with that, because it does not correctly copy properties with non-default attributes.
If an object for example has just a setter, it’s not correctly copied to a new object, using Object.assign()
. For example with this object:
const person1 = { set name(newName) { console.log(newName) } }
This copy attempt won’t work:
const person2 = {} Object.assign(person2, person1)
But this will work and copy over the setter correctly:
const person3 = {}
Object.defineProperties(person3, Object.getOwnPropertyDescriptors(person1))
As you can see with a console test:
person1.name = 'x'
"x"
person2.name = 'x'
person3.name = 'x'
"x"
person2
misses the setter, it was not copied over.
The same limitation goes for shallow cloning objects with Object.create()
.