Skip to content

Commit 5879188

Browse files
authored
Non-extensible objects can be created by other methods (#41676)
1 parent 6ef7bc0 commit 5879188

File tree

1 file changed

+22
-20
lines changed
  • files/en-us/web/javascript/reference/errors/cant_define_property_object_not_extensible

1 file changed

+22
-20
lines changed

files/en-us/web/javascript/reference/errors/cant_define_property_object_not_extensible/index.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ page-type: javascript-error
55
sidebar: jssidebar
66
---
77

8-
The JavaScript exception "can't define property "x": "obj" is not extensible" occurs
9-
when {{jsxref("Object.preventExtensions()")}} marked an object as no longer extensible,
10-
so that it will never have properties beyond the ones it had at the time it was marked
11-
as non-extensible.
8+
The JavaScript exception "can't define property "x": "obj" is not extensible" occurs when an object is marked as non-extensible, so that it will never have properties beyond the ones it had at the time it was marked as non-extensible. Objects can be made non-extensible by calling {{jsxref("Object.preventExtensions()")}}, {{jsxref("Object.seal()")}}, or {{jsxref("Object.freeze()")}}.
129

1310
## Message
1411

@@ -25,19 +22,13 @@ TypeError: Attempting to define property on object that is not extensible. (Safa
2522

2623
## What went wrong?
2724

28-
Usually, an object is extensible and new properties can be added to it. However, in
29-
this case {{jsxref("Object.preventExtensions()")}} marked an object as no longer
30-
extensible, so that it will never have properties beyond the ones it had at the time it
31-
was marked as non-extensible.
25+
Usually, an object is extensible and new properties can be added to it. However, in this case the object isn't extensible, so that it will never have properties beyond the ones it had at the time it was marked as non-extensible. You could have marked the object as non-extensible by calling {{jsxref("Object.preventExtensions()")}}, {{jsxref("Object.seal()")}}, or {{jsxref("Object.freeze()")}}, or a library you are using could have done that for you.
3226

3327
## Examples
3428

3529
### Adding new properties to a non-extensible objects
3630

37-
In [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode),
38-
attempting to add new properties to a non-extensible object throws a
39-
`TypeError`. In sloppy mode, the addition of the "x" property is silently
40-
ignored.
31+
In [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode), attempting to add new properties to a non-extensible object via assignment throws a `TypeError`. In sloppy mode, the addition of the "x" property is silently ignored.
4132

4233
```js example-bad
4334
"use strict";
@@ -49,9 +40,7 @@ obj.x = "foo";
4940
// TypeError: can't define property "x": Object is not extensible
5041
```
5142

52-
In both, [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode) and
53-
sloppy mode, a call to {{jsxref("Object.defineProperty()")}} throws when adding a new
54-
property to a non-extensible object.
43+
In both [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode) and sloppy mode, a call to {{jsxref("Object.defineProperty()")}} throws when adding a new property to a non-extensible object.
5544

5645
```js example-bad
5746
const obj = {};
@@ -61,11 +50,22 @@ Object.defineProperty(obj, "x", { value: "foo" });
6150
// TypeError: can't define property "x": Object is not extensible
6251
```
6352

64-
To fix this error, you will either need to remove the call to
65-
{{jsxref("Object.preventExtensions()")}} entirely, or move it to a position so that the
66-
property is added earlier and only later the object is marked as non-extensible. Of
67-
course you can also remove the property that was attempted to be added, if you don't
68-
need it.
53+
### Non-extensible objects created by other means
54+
55+
The `Object.seal()` and `Object.freeze()` methods also create non-extensible objects—they just have additional restrictions on modifying existing properties as well.
56+
57+
```js example-bad
58+
"use strict";
59+
60+
const obj = { y: "bar" };
61+
Object.seal(obj);
62+
obj.x = "foo";
63+
// TypeError: can't define property "x": Object is not extensible
64+
```
65+
66+
### Fixing the error
67+
68+
There are three ways to fix this error: you can remove the property addition entirely if you don't need it, you can copy the existing properties to a new extensible object, or you can add the property before making the object non-extensible.
6969

7070
```js example-good
7171
"use strict";
@@ -79,3 +79,5 @@ Object.preventExtensions(obj);
7979
## See also
8080

8181
- {{jsxref("Object.preventExtensions()")}}
82+
- {{jsxref("Object.seal()")}}
83+
- {{jsxref("Object.freeze()")}}

0 commit comments

Comments
 (0)