Get All Property Descriptors at Once
You'll learn how to retrieve descriptors for all own properties of an object in a single operation.
What It Does
In the previous lesson, you learned about Object.getOwnPropertyDescriptor(), which retrieves the descriptor for one property at a time. Object.getOwnPropertyDescriptors() (note the plural) does the same thing for every own property simultaneously, returning an object where each key is a property name and each value is that property's descriptor.
Think of it like getting a complete medical chart for a patient instead of requesting one test result at a time—you get the whole picture in one go.
Syntax
const person = {
name: 'Alice',
age: 30
};
Object.defineProperty(person, 'id', {
value: 12345,
writable: false,
enumerable: false,
configurable: false
});
const allDescriptors = Object.getOwnPropertyDescriptors(person);
console.log(allDescriptors);
/*
{
name: { value: 'Alice', writable: true, enumerable: true, configurable: true },
age: { value: 30, writable: true, enumerable: true, configurable: true },
id: { value: 12345, writable: false, enumerable: false, configurable: false }
}
*/
Why It's Useful
This method is particularly helpful when you need to:
- Copy an object with exact descriptor settings preserved
- Inspect all property configurations at once for debugging
- Clone objects more accurately than simple spread operators
It captures the complete state of an object's property definitions, including non-enumerable properties that other methods might miss.
Key Takeaway: Object.getOwnPropertyDescriptors() returns an object containing descriptors for all own properties, giving you a complete snapshot of an object's property configurations in one call.