-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
83 lines (59 loc) · 2.26 KB
/
index.md
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
---
title: 'TypeError: "x" is read-only'
slug: Web/JavaScript/Reference/Errors/Read-only
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode)-only exception
"is read-only" occurs when a global variable or object
property that was assigned to is a read-only property.
## Message
```plain
TypeError: Cannot assign to read only property 'x' of #<Object> (V8-based)
TypeError: "x" is read-only (Firefox)
TypeError: Attempted to assign to readonly property. (Safari)
```
## Error type
{{jsxref("TypeError")}} in [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode) only.
## What went wrong?
The global variable or object property that was assigned to is a read-only property.
(Technically, it is a [non-writable data property](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#writable_attribute).)
This error happens only in [strict mode code](/en-US/docs/Web/JavaScript/Reference/Strict_mode). In
non-strict code, the assignment is silently ignored.
## Examples
### Invalid cases
Read-only properties are not super common, but they can be created using
{{jsxref("Object.defineProperty()")}} or {{jsxref("Object.freeze()")}}.
```js example-bad
"use strict";
const obj = Object.freeze({ name: "Elsa", score: 157 });
obj.score = 0; // TypeError
("use strict");
Object.defineProperty(this, "LUNG_COUNT", { value: 2, writable: false });
LUNG_COUNT = 3; // TypeError
("use strict");
const frozenArray = Object.freeze([0, 1, 2]);
frozenArray[0]++; // TypeError
```
There are also a few read-only properties built into JavaScript. Maybe you tried to
redefine a mathematical constant.
```js example-bad
"use strict";
Math.PI = 4; // TypeError
```
Sorry, you can't do that.
The global variable `undefined` is also read-only, so you can't silence the
infamous "undefined is not a function" error by doing this:
```js example-bad
"use strict";
undefined = function () {}; // TypeError: "undefined" is read-only
```
### Valid cases
```js example-good
"use strict";
let obj = Object.freeze({ name: "Score", points: 157 });
obj = { name: obj.name, points: 0 }; // replacing it with a new object works
```
## See also
- {{jsxref("Object.defineProperty()")}}
- {{jsxref("Object.freeze()")}}