Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
JavaScript Object.getOwnPropertyDescriptors()
In this tutorial, you will learn about the JavaScript Object.getOwnPropertyDescriptors() method with the help of examples.
The Object.getOwnPropertyDescriptors() method returns the property descriptors for all the properties of the given object.
Example
let obj = {
value: 11,
get number() {
return this.value;
},
};
// get property descriptors for all the properties of obj
let objectProperties = Object.getOwnPropertyDescriptors(obj);
console.log(objectProperties);
Output
{
value: { value: 11, writable: true, enumerable: true, configurable: true },
number: {
get: [Function: get number],
set: undefined,
enumerable: true,
configurable: true
}
}
getOwnPropertyDescriptors() Syntax
The syntax of the getOwnPropertyDescriptors() method is:
Object.getOwnPropertyDescriptors(obj)
Here, getOwnPropertyDescriptors() is a static method. Hence, we need to access the method using the class name, Object.
getOwnPropertyDescriptors() Parameters
The getOwnPropertyDescriptors() method takes in:
- obj - the object whose property descriptors we require.
getOwnPropertyDescriptors() Return Value
The getOwnPropertyDescriptors() method returns an object containing all the property descriptors of the given object.
Example 1: JavaScript Object.getOwnPropertyDescriptors()
let obj = {
x: 10,
get number() {
return this.x;
},
};
// get the property descriptors for all the properties of obj
let value = Object.getOwnPropertyDescriptors(obj);
console.log(value);
Output
{
x: { value: 10, writable: true, enumerable: true, configurable: true },
number: {
get: [Function: get number],
set: undefined,
enumerable: true,
configurable: true
}
}
In the above example, the obj object contains the following properties:
- x - a property with a value of 10.
number()- a getter method named which returns the value of x.
We then used the Object.getOwnPropertyDescriptors() method to get the descriptors for all the properties of obj i.e. x and number().
Example 2: getOwnPropertyDescriptors() for Shallow Copy
let obj = {
x: 10,
get number() {
return this.x;
},
};
// use getOwnPropertyDescriptors() for shallow copy
let cloneObj = Object.create(
Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj)
);
console.log(cloneObj);
// Output: { x: 10, number: [Getter] }
In the above example, we can see that the getOwnPropertyDescriptors() method along with the create() method can help us create a shallow copy.
To demonstrate this, we first created an object named obj with the properties:
- x - a property with the value 10.
number()- a getter method that returns the value of x.
Then, we created a shallow copy of obj (called cloneObj) using the create(), getPrototypeOf(), and getOwnPropertyDescriptors() methods.
let cloneObj = Object.create(
Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj)
);
As shown by the output, cloneObj is a shallow copy of obj since both objects are the same (they point to the same memory address).
Also Read: