aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/read-only/index.md
diff options
context:
space:
mode:
authorMasahiro FUJIMOTO <mfujimot@gmail.com>2021-08-16 09:54:35 +0900
committerGitHub <noreply@github.com>2021-08-16 09:54:35 +0900
commit816ac814e8a11897db45dfad83cfdd9d3dae55fb (patch)
tree2f3f1bd89c6ad7711a2b02dc00c240c7e6d07e2f /files/ja/web/javascript/reference/errors/read-only/index.md
parent4ad4ec00bd93c2344f9e8141508dce0baf22e00a (diff)
downloadtranslated-content-816ac814e8a11897db45dfad83cfdd9d3dae55fb.tar.gz
translated-content-816ac814e8a11897db45dfad83cfdd9d3dae55fb.tar.bz2
translated-content-816ac814e8a11897db45dfad83cfdd9d3dae55fb.zip
Web/JavaScript/Reference/Errors以下の5文書をMarkdown化 (#1971)
- 5文書をMarkdown化 - 2021/08/07時点の最新版に同期
Diffstat (limited to 'files/ja/web/javascript/reference/errors/read-only/index.md')
-rw-r--r--files/ja/web/javascript/reference/errors/read-only/index.md86
1 files changed, 86 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/errors/read-only/index.md b/files/ja/web/javascript/reference/errors/read-only/index.md
new file mode 100644
index 0000000000..4e6ea7f36b
--- /dev/null
+++ b/files/ja/web/javascript/reference/errors/read-only/index.md
@@ -0,0 +1,86 @@
+---
+title: 'TypeError: "x" is read-only'
+slug: Web/JavaScript/Reference/Errors/Read-only
+tags:
+ - Error
+ - Errors
+ - JavaScript
+ - TypeError
+translation_of: Web/JavaScript/Reference/Errors/Read-only
+---
+{{jsSidebar("Errors")}}
+
+JavaScript の [strict モード](/ja/docs/Web/JavaScript/Reference/Strict_mode)のみの例外 "is read-only" は、代入されたグローバル変数またはオブジェクトプロパティが読み取り専用プロパティであった場合に発生します。
+
+## エラーメッセージ
+
+```js
+TypeError: Assignment to read-only properties is not allowed in strict mode (Edge)
+TypeError: "x" is read-only (Firefox)
+TypeError: 0 is read-only (Firefox)
+TypeError: Cannot assign to read only property 'x' of #<Object> (Chrome)
+TypeError: Cannot assign to read only property '0' of [object Array] (Chrome)
+```
+
+## エラーの種類
+
+{{jsxref("TypeError")}}
+
+## エラーの原因
+
+代入しようとしたグローバル変数、またはオブジェクトのプロパティが読み取り専用プロパティです。 (技術的には、 [non-writable データプロパティ](/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Writable_attribute)です。)
+
+このエラーは、 [strict モードのコード](/en-US/docs/Web/JavaScript/Reference/Strict_mode)にだけ発生します。 strict コードではない場合、割り当ては無視されるだけです。</p>
+
+## 例
+
+### 無効な場合
+
+<p>読み取り専用プロパティはさほど一般的ではありませんが、 {{jsxref("Object.defineProperty()")}}、または {{jsxref("Object.freeze()")}} を使用して生成することができます。</p>
+
+```js example-bad
+'use strict';
+var 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';
+var frozenArray = Object.freeze([0, 1, 2]);
+frozenArray[0]++; // TypeError
+```
+
+JavaScript の組み込みにも、いくつか読み取り専用プロパティがあります。数学的な定数を再定義しようとしたとします。
+
+```js example-bad
+'use strict';
+Math.PI = 4; // TypeError
+```
+
+残念ながらできません。
+
+グローバル変数の `undefined` も読み取り専用のため、このようにすると悪名高い "undefined is not a function" エラーが発生します。
+
+```js example-bad
+'use strict';
+undefined = function() {}; // TypeError: "undefined" is read-only
+```
+
+### 有効な場合
+
+```js example-good
+'use strict';
+var obj = Object.freeze({name: 'Score', points: 157});
+obj = {name: obj.name, points: 0}; // 新しいオブジェクトで置き換える
+
+'use strict';
+var LUNG_COUNT = 2; // `var` が使われているので、読み取り専用ではない
+LUNG_COUNT = 3; // ok (解剖学的にはおかしいけれども)
+```
+
+## 関連情報
+
+- {{jsxref("Object.defineProperty()")}}
+- {{jsxref("Object.freeze()")}}