.getOwnPropertyDescriptors()

Published Dec 15, 2023
Contribute to Docs

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
}

All contributors

Looking to contribute?

Learn JavaScript on Codecademy