JavaScript(JS) object method - getOwnPropertyDescriptor

In JavaScript, the Object.getOwnPropertyDescriptor() method is used to get the descriptor for a property of an object. The descriptor is an object that contains information about the property, such as whether it's writable, configurable, and enumerable.

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

re‮ ref‬to:theitroad.com
Object.getOwnPropertyDescriptor(obj, prop)

Where obj is the object that contains the property, and prop is the name of the property whose descriptor you want to retrieve.

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

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

const descriptor = Object.getOwnPropertyDescriptor(person, 'age');

console.log(descriptor);
/* Output:
{
  value: 30,
  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.getOwnPropertyDescriptor() on the person object to get the descriptor for the age property. The resulting descriptor object contains information about the age property, such as its value (30), and whether it's writable, enumerable, and configurable.

It's important to note that Object.getOwnPropertyDescriptor() only works with own properties of an object, and not inherited properties. If the property doesn't exist on the object, Object.getOwnPropertyDescriptor() will return undefined. Also, the descriptor returned by Object.getOwnPropertyDescriptor() is read-only, which means that you cannot modify it directly. If you want to modify the descriptor for a property, you'll need to use the Object.defineProperty() or Object.defineProperties() method.