creation
Object.defineProperties
Defines multiple new or modified properties on an object by supplying a descriptors map. The batch-form counterpart of Object.defineProperty.
Object.defineProperties(obj, propsObject) Parameters
| Parameter | Purpose |
|---|---|
| obj | The target object |
| propsObject | An object whose keys are property names and values are property descriptors |
| returns | The mutated object |
Examples
const o = {};
Object.defineProperties(o, {
a: { value: 1, enumerable: true },
b: { value: 2, enumerable: true }
});
console.log(o); Logs { a: 1, b: 2 }
const o = {};
Object.defineProperties(o, { hidden: { value: 42 } });
console.log(Object.keys(o), o.hidden); Logs [] 42 — hidden is non-enumerable by default
const src = { a: 1, b: 2 };
const clone = Object.defineProperties({}, Object.getOwnPropertyDescriptors(src));
console.log(clone); Logs { a: 1, b: 2 } — descriptor-preserving clone
const o = {};
Object.defineProperties(o, {
full: { get() { return `${this.first} ${this.last}`; }, enumerable: true },
first: { value: 'Ada', enumerable: true },
last: { value: 'Lovelace', enumerable: true }
});
console.log(o.full); Logs 'Ada Lovelace'
Gotcha
Same descriptor defaults as defineProperty — omitted booleans are false. If any single descriptor is invalid the operation throws, but previously-processed descriptors are NOT rolled back.
Related methods
Object.defineProperty
Defines a new property directly on an object, or modifies an existing one, with fine-grained control over its descriptor. Returns the object.
Object.getOwnPropertyDescriptor
Returns the property descriptor for an own property on the given object, or undefined if the property does not exist. Descriptors describe writability, enumerability, configurability, and getters/setters.
Object.create
Creates a new object with the specified prototype and optional property descriptors. Pass null to create an object with no prototype at all.
Object.fromEntries
Transforms a list of key-value pairs into an object — the inverse of Object.entries. Introduced in ES2019 and accepts any iterable of [key, value] pairs, including Map.
Object.assign
Copies all enumerable own properties from one or more source objects to a target object, then returns the modified target. Performs a shallow merge, invoking source getters and target setters.