aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/errors/read-only
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/errors/read-only
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/errors/read-only')
-rw-r--r--files/zh-cn/web/javascript/reference/errors/read-only/index.html76
1 files changed, 76 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/errors/read-only/index.html b/files/zh-cn/web/javascript/reference/errors/read-only/index.html
new file mode 100644
index 0000000000..ecdc225497
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/errors/read-only/index.html
@@ -0,0 +1,76 @@
+---
+title: 'TypeError: "x" is read-only'
+slug: Web/JavaScript/Reference/Errors/Read-only
+translation_of: Web/JavaScript/Reference/Errors/Read-only
+---
+<div>{{jsSidebar("Errors")}}</div>
+
+<h2 id="报错消息">报错消息</h2>
+
+<pre class="syntaxbox">TypeError: "x" is read-only (Firefox) //格式错误:"x"只读。(x可以代表任意值)
+TypeError: 0 is read-only (Firefox)
+TypeError: Cannot assign to read only property 'x' of #&lt;Object&gt; (Chrome)
+//格式错误:对象的x属性是只读的不能设置 (chrome)
+TypeError: Cannot assign to read only property '0' of [object Array] (Chrome)
+</pre>
+
+<h2 id="错误格式">错误格式</h2>
+
+<p>{{jsxref("TypeError")}}</p>
+
+<h2 id="哪里出错了">哪里出错了?</h2>
+
+<p>全局变量或对象属性被设置为只读 (专业点讲,就是这条数据属性禁止写入.)</p>
+
+<p>这条错误值发生在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode code</a>(俗称严格模式). 正常情况下,是没有报错的。</p>
+
+<h2 id="例如">例如</h2>
+
+<h3 id="无效例子(也就是下面这么做会导致报这种错)">无效例子(也就是下面这么做会导致报这种错)</h3>
+
+<p>只读属性不能直接创建, 但我们可以通过{{jsxref("Object.defineProperty()")}} 或 {{jsxref("Object.freeze()")}}设置.</p>
+
+<pre class="brush: 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内置属性. 如果你尝试修改一个常量.
+
+"use strict";
+Math.PI = 4; // TypeError</pre>
+
+<p>傻了吧,报错了</p>
+
+<p><code>全局变量undefined也是只读的</code>, 所以你不能忽视臭名昭著的"undefined is not a function"错误:</p>
+
+<pre class="brush: js example-bad">"use strict";
+undefined = function () {}; // TypeError: "undefined" is read-only
+</pre>
+
+<h3 id="下面这样都是有效,不报错的">下面这样都是有效,不报错的</h3>
+
+<pre class="brush: 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; //
+LUNG_COUNT = 3; //
+</pre>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>{{jsxref("Object.defineProperty()")}}</li>
+ <li>{{jsxref("Object.freeze()")}}</li>
+ <li><a href="https://www.answers.com/Q/Which_animals_have_three_lungs">"Which animals have three lungs?" on answers.com</a></li>
+ <li><a href="https://aliens.wikia.com/wiki/Klingon">Klingons</a> (another answer to that query)</li>
+</ul>