The Object.getOwnPropertyDescriptor()
method returns a property descriptor for a specific property of an object.
Example
let obj = {num: 10}
// get the property description
// of the num property of obj
let numValue = Object.getOwnPropertyDescriptor(obj, "num");
console.log(numValue);
// Output: { value: 10, writable: true, enumerable: true, configurable: true }
getOwnPropertyDescriptor() Syntax
The syntax of the getOwnPropertyDescriptor()
method is:
Object.getOwnPropertyDescriptor(obj, prop)
Here, getOwnPropertyDescriptor()
is a static method. Hence, we need to access the method using the class name, Object
.
getOwnPropertyDescriptor() Parameters
The getOwnPropertyDescriptor()
method takes in:
- obj - the object in which to look for the property.
- prop - the name or Symbol of the property whose description is to be retrieved.
getOwnPropertyDescriptor() Return Value
The getOwnPropertyDescriptor()
method returns:
- a property descriptor of the specified property of the object.
undefined
if the property does not exist in the object.
Example 1: JavaScript Object.getOwnPropertyDescriptor()
let obj = {
x: 711,
get number() {
return this.x;
},
};
// get property description of x in obj
let xDescriptors = Object.getOwnPropertyDescriptor(obj, "x");
console.log(xDescriptors);
// get property description of number() method
let value = Object.getOwnPropertyDescriptor(obj, "number");
console.log(value);
Output
{ value: 711, writable: true, enumerable: true, configurable: true } { get: [Function: get number], set: undefined, enumerable: true, configurable: true }
In this program, we have created an object obj with the following properties:
- x - a property with the value 711
number()
- aget
method that returns the value of x
Then, we used the getOwnPropertyDescriptor()
method to find the property descriptors of x and number()
:
// find property description of x
let xDescriptors = Object.getOwnPropertyDescriptor(obj, "x");
// find property description of number() method
let value = Object.getOwnPropertyDescriptor(obj, "number");
Notice the arguments given to the getOwnPropertyDescriptor()
method. The object name obj is written without quotation marks.
However, the property names x and number
are written inside double quotes i.e. "x"
and "number"
.
Example 2: getOwnPropertyDescriptor() With defineProperty()
let obj={}
// define a property of obj using
// the defineProperty() method
Object.defineProperty(obj, "id", {
value: 123,
writable: false,
enumerable: false,
});
// find property description of created property 'id'
console.log(Object.getOwnPropertyDescriptor(obj, "id"));
Output
{ value: 123, writable: false, enumerable: false, configurable: false }
In the above example, we have initialized an empty object obj. Then, we defined a single property id on it using the defineProperty()
method.
Finally, we printed all the property descriptions of the id property with the help of getOwnPropertyDescriptor()
.
The resulting output shows that getOwnPropertyDescriptor()
returns the same property descriptors that we had defined using defineProperty()
.
Also Read: