Object.defineProperties()
Defines or modifies multiple properties on an object using descriptors.
Syntax
Object.defineProperties(obj, descriptors) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
obj | object | Yes | The object on which to define the properties. |
descriptors | object | Yes | An object whose keys are property names and values are property descriptors. |
Returns
object — The object that was passed in.
Throws
TypeError— a descriptor is invalid.
Examples
const o = {};
Object.defineProperties(o, {
a: { value: 1, enumerable: true },
b: { value: 2 }
});
console.log(o.a, o.b, Object.keys(o));
Output
1 2 [ 'a' ]
const o = {};
Object.defineProperties(o, { x: { get: () => 9 } });
console.log(o.x);
Output
9
Notes
Equivalent to calling `Object.defineProperty` for each entry. Omitted
attributes default to false. The descriptors object itself must be enumerable.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 5 |
| firefox | 4 |
| safari | 5.1 |
| edge | 12 |
| node | 0.10 |