The Object.getOwnPropertyDescriptor function enables users to retrieve comprehensive details regarding a property and provides a property descriptor for an own property (meaning one that is directly located on an object rather than being inherited from the object's prototype chain) of a specified object.
Syntax:
Example
bject.getOwnPropertyDescriptor(obj, prop)
Parameter
obj: This refers to the object in which the property is to be searched for.
Prop: This term refers to the designation of the property for which the description is intended to be obtained.
Return value:
It provides a property descriptor for the specified property if that property is present on the object.
Browser Support:
| Chrome | 4 |
|---|---|
Edge |
Yes |
| Firefox | 12 |
| Opera | 4 |
Example 1
Example
const object1 = {
property1: 42
}
const object2 = {
property2: 34
}
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
const descriptor2 = Object.getOwnPropertyDescriptor(object2, 'property2');
console.log(descriptor1.enumerable);
console.log(descriptor2.enumerable);
console.log(descriptor1.value);
console.log(descriptor2.value);
Output:
Output
true
true
42
34
Example 2
Example
const object1 = {
property1: 42
}
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
console.log(descriptor1.configurable);
console.log(descriptor1.enumerable);
console.log(descriptor1.value);
Output:
Output
true
true
42
Example 3
Example
const object1 = {
property1: 56
}
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
console.log(descriptor1.writable);
console.log(descriptor1.value);
Output:
Output
true
56