JavaScript(JS) object method - getOwnPropertyDescriptors

http‮.www//:s‬theitroad.com

In JavaScript, the Object.getOwnPropertyDescriptors() method is used to get all property descriptors for an object. The descriptors are returned in an object with each property's name as the key, and the descriptor object as the value.

The syntax for using Object.getOwnPropertyDescriptors() is as follows:

Object.getOwnPropertyDescriptors(obj)

Where obj is the object whose property descriptors you want to retrieve.

Here's an example of using Object.getOwnPropertyDescriptors():

const person = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const descriptors = Object.getOwnPropertyDescriptors(person);

console.log(descriptors);
/* Output:
{
  name: {
    value: "John",
    writable: true,
    enumerable: true,
    configurable: true
  },
  age: {
    value: 30,
    writable: true,
    enumerable: true,
    configurable: true
  },
  city: {
    value: "New York",
    writable: true,
    enumerable: true,
    configurable: true
  }
}
*/

In this example, we create an object called person with three properties: name, age, and city. We then call Object.getOwnPropertyDescriptors() on the person object to get all of its property descriptors. The resulting descriptors object contains information about all the properties of the person object, such as their values, and whether they're writable, enumerable, and configurable.

It's important to note that Object.getOwnPropertyDescriptors() only works with own properties of an object, and not inherited properties. If the object has no own properties, an empty object will be returned. Also, the descriptors returned by Object.getOwnPropertyDescriptors() are read-only, which means that you cannot modify them directly. If you want to modify the descriptors for a property, you'll need to use the Object.defineProperty() or Object.defineProperties() method.