.getOwnPropertyDescriptors()
The .getOwnPropertyDescriptors()
method returns all own property descriptors of a given object. It is useful for extracting property descriptors, including configurable, enumerable, value, and writable properties, allowing manipulation of these property attributes from an object.
Syntax
Object.getOwnPropertyDescriptors(obj)
obj
: It is the given object.
Examples
Example 1
In this example, Object.getOwnPropertyDescriptors()
returns an object where each key is a property name, and the corresponding value is the property descriptor for that property:
const obj = {name: 'John',age: 25,};const propertyDescriptors = Object.getOwnPropertyDescriptors(obj);console.log(propertyDescriptors);
The above code snippet will return the following output:
{name: { value: 'John', writable: true, enumerable: true, configurable: true },age: { value: 25, writable: true, enumerable: true, configurable: true }}
Example 2
In this example, clonedObject
is a new object with the same property descriptors as originalObject
. getOwnPropertyDescriptors
ensures that the cloned object has the same property attributes, including reconfigurability and enumerability.
const originalObject = {name: 'John',age: 25,};const clonedObject = Object.create(Object.getPrototypeOf(originalObject),Object.getOwnPropertyDescriptors(originalObject));console.log(clonedObject);
The above code snippet will return the following output:
{name: 'John',age: 25}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Skill path
Create a Back-End App with JavaScript
Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.Includes 8 CoursesWith CertificateBeginner Friendly30 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours