The Object.getOwnPropertyDescriptors function retrieves all the own property descriptors for a specified object. In contrast to the getOwnPropertyDescriptor method, which returns a single descriptor, the getOwnPropertyDescriptors method does not take symbolic properties into account.
Syntax:
Example
Object.getOwnPropertyDescriptors(obj)
Parameter
obj: This refers to the object from which all of its own property descriptors are to be retrieved.
Return:
This function yields an object that includes all the own property descriptors of a given object. It is possible for this function to return an empty object in the event that there are no properties present.
Browser Support:
| Chrome | 54 |
|---|---|
Edge |
15 |
| Firefox | 50 |
| Opera | 41 |
Example 1
Example
const object1 = {
property1: 103
};
const descriptors1 = Object.getOwnPropertyDescriptors(object1);
console.log(descriptors1.property1.writable);
console.log(descriptors1.property1.value);
Output:
Example 2
Example
const object1 = {
property1: 22
};
const descriptors1 = Object.getOwnPropertyDescriptors(object1);
console.log(descriptors1.property1.value);
console.log(descriptors1.property1);
console.log(descriptors1.property1.writable);
Output:
Output
[object Object] {
configurable: true,
enumerable: true,
value: 22,
writable: true
}
true
Example 3
Example
const object1 = {
property1: 42
};
const object2 = {
property2: 23
};
const descriptors1 = Object.getOwnPropertyDescriptors(object1);
const descriptors2 = Object.getOwnPropertyDescriptors(object2);
console.log(descriptors1.property1.writable);
console.log(descriptors1.property1.value,descriptors2.property2.value);
Output:
Output
true
42
23