File size: 3,757 Bytes
780c9fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
---
title: Object.defineProperties()
short-title: defineProperties()
slug: Web/JavaScript/Reference/Global_Objects/Object/defineProperties
page-type: javascript-static-method
browser-compat: javascript.builtins.Object.defineProperties
sidebar: jsref
---
The **`Object.defineProperties()`** static method defines new or
modifies existing properties directly on an object, returning the object.
{{InteractiveExample("JavaScript Demo: Object.defineProperties()")}}
```js interactive-example
const object = {};
Object.defineProperties(object, {
property1: {
value: 42,
writable: true,
},
property2: {},
});
console.log(object.property1);
// Expected output: 42
```
## Syntax
```js-nolint
Object.defineProperties(obj, props)
```
### Parameters
- `obj`
- : The object on which to define or modify properties.
- `props`
- : An object whose keys represent the names of properties to be defined or modified and
whose values are objects describing those properties. Each value in `props`
must be either a data descriptor or an accessor descriptor; it cannot be both (see
{{jsxref("Object.defineProperty()")}} for more details).
Data descriptors and accessor descriptors may optionally contain the following keys:
- `configurable`
- : `true` if and only if the type of this property descriptor may be
changed and if the property may be deleted from the corresponding object.
**Defaults to `false`.**
- `enumerable`
- : `true` if and only if this property shows up during enumeration of
the properties on the corresponding object.
**Defaults to `false`.**
A data descriptor also has the following optional keys:
- `value`
- : The value associated with the property. Can be any valid JavaScript value
(number, object, function, etc.).
**Defaults to {{jsxref("undefined")}}.**
- `writable`
- : `true` if and only if the value associated with the property may be
changed with an {{jsxref("Operators", "assignment operator", "assignment_operators", 1)}}.
**Defaults to `false`.**
An accessor descriptor also has the following optional keys:
- `get`
- : A function which serves as a getter for the property, or {{jsxref("undefined")}}
if there is no getter. The function's return value will be used as the value of
the property.
**Defaults to {{jsxref("undefined")}}.**
- `set`
- : A function which serves as a setter for the property, or {{jsxref("undefined")}}
if there is no setter. The function will receive as its only argument the new
value being assigned to the property.
**Defaults to {{jsxref("undefined")}}.**
If a descriptor has neither of `value`, `writable`,
`get` and `set` keys, it is treated as a data descriptor. If a
descriptor has both `value` or `writable` and `get`
or `set` keys, an exception is thrown.
### Return value
The object that was passed to the function.
## Examples
### Using Object.defineProperties
```js
const obj = {};
Object.defineProperties(obj, {
property1: {
value: true,
writable: true,
},
property2: {
value: "Hello",
writable: false,
},
// etc. etc.
});
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Polyfill of `Object.defineProperties` in `core-js`](https://github.com/zloirock/core-js#ecmascript-object)
- [es-shims polyfill of `Object.defineProperties`](https://www.npmjs.com/package/object.defineproperties)
- {{jsxref("Object.defineProperty()")}}
- {{jsxref("Object.keys()")}}
- [Enumerability and ownership of properties](/en-US/docs/Web/JavaScript/Guide/Enumerability_and_ownership_of_properties)
|