You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: files/en-us/web/javascript/reference/errors/cant_define_property_object_not_extensible/index.md
+22-20Lines changed: 22 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,7 @@ page-type: javascript-error
5
5
sidebar: jssidebar
6
6
---
7
7
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()")}}.
12
9
13
10
## Message
14
11
@@ -25,19 +22,13 @@ TypeError: Attempting to define property on object that is not extensible. (Safa
25
22
26
23
## What went wrong?
27
24
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.
32
26
33
27
## Examples
34
28
35
29
### Adding new properties to a non-extensible objects
36
30
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.
41
32
42
33
```js example-bad
43
34
"use strict";
@@ -49,9 +40,7 @@ obj.x = "foo";
49
40
// TypeError: can't define property "x": Object is not extensible
50
41
```
51
42
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.
// TypeError: can't define property "x": Object is not extensible
62
51
```
63
52
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
+
constobj= { 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.
0 commit comments