Skip to content
Tutorials Center
My learning

Topics

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().

JavaScript
Object.defineProperties(obj, {
  a: { value: 1, enumerable: true },
  b: { value: 2 },
});

Try it

JS Several properties at once

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Getters from a list of fields

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 are false (or undefined for value, get and set). 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 props count. 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/set syntax) 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.