JavaScript Reference ยท Object
Object.defineProperties()
Reference for JavaScript Object.defineProperties(): define or change several properties in one call using a map of property descriptors, with examples.
Object.defineProperties() is the multi-property version of Object.defineProperty(). You pass an object whose keys are property names and whose values are property descriptors, and every property is defined on the target in one call.
It is useful when you build an object with several read-only or hidden members, or when you generate properties from data. The same descriptor format is used by the second argument of Object.create().
Object.defineProperties(obj, { a: { value: 1, enumerable: true }, b: { value: 2 }, });
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Object.defineProperties(obj, props)
| Name | Description |
|---|---|
obj |
The object to change. |
props |
An object whose own enumerable properties (strings and symbols) are descriptors for the properties to define. |
Return value: the object obj, changed in place.
Exceptions: a TypeError if obj is not an object, if any descriptor is invalid, or if a change is not permitted on an existing non-configurable property.
Usage notes
- Same rules as
defineProperty(). For a new property, omitted attributes arefalse(orundefinedforvalue,getandset). For an existing one, omitted attributes keep their present values. - Not atomic. The properties are defined one after another. If a later descriptor is invalid and throws, the earlier ones have already been applied.
- Only own enumerable keys of
propscount. Inherited keys and non-enumerable keys of the descriptor map are ignored. - It does not copy values from another object. To copy properties including getters, build descriptors with
Object.getOwnPropertyDescriptors()and pass them here.Object.assign()runs getters and copies their values instead. - Prefer object literals (with
get/setsyntax) when plain data and accessors are enough. Reach for descriptors when you need the stricter attributes.
Browser support
Object.defineProperties() is available in every browser and JavaScript runtime.