Object.defineProperties
What you'll learn: How to define or modify multiple properties on an object at once, each with its own custom descriptor settings.
The Core Idea
In the previous lesson, you learned Object.defineProperty to configure a single property with precise control over writable, enumerable, and configurable. But what if you need to set up multiple properties at once? That's where Object.defineProperties comes in—it's the plural version that handles multiple properties in a single call.
How It Works
The syntax takes two arguments: the target object and an object containing all the property definitions:
const user = {};
Object.defineProperties(user, {
name: {
value: 'Alice',
writable: true,
enumerable: true,
configurable: true
},
age: {
value: 30,
writable: false,
enumerable: true,
configurable: false
},
id: {
value: 'U12345',
writable: false,
enumerable: false,
configurable: false
}
});
console.log(user.name); // "Alice"
user.age = 31; // Fails silently (or throws in strict mode)
console.log(Object.keys(user)); // ["name", "age"] — 'id' is hidden
Why Use This?
Think of it as batch processing for object setup. Instead of calling Object.defineProperty three times, you configure everything in one go. This is especially useful when initializing objects with specific visibility or immutability rules.
The Pattern
Each property name maps to a descriptor object with the same options you used before: value, writable, enumerable, and configurable. You're essentially passing a "blueprint" for multiple properties at once.
Key Takeaway: Object.defineProperties lets you define or modify multiple properties simultaneously, each with its own descriptor configuration—cleaner and more efficient than repeated single-property calls.