55 lines
4.0 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="报错消息">报错消息</h2>
<pre><code class="language-javascript">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)
</code></pre>
<h2 id="错误格式">错误格式</h2>
<p><a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></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>只读属性不能直接创建, 但我们可以通过<a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a><a href="Reference/Global_Objects/Object/freeze" title="Object.freeze() 方法可以冻结一个对象,冻结指的是不能向这个对象添加新的属性,不能修改其已有属性的值,不能删除已有属性,以及不能修改该对象已有属性的可枚举性、可配置性、可写性。该方法返回被冻结的对象。"><code>Object.freeze()</code></a>设置.</p>
<pre><code class="language-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</code></pre>
<p>傻了吧,报错了</p>
<p><code>全局变量undefined也是只读的</code>, 所以你不能忽视臭名昭著的"undefined is not a function"错误:</p>
<pre><code class="language-js example-bad">"use strict";
undefined = function () {}; // TypeError: "undefined" is read-only
</code></pre>
<h3 id="下面这样都是有效,不报错的">下面这样都是有效,不报错的</h3>
<pre><code class="language-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; //
</code></pre>
<h2 id="参见">参见</h2>
<ul>
<li><a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a></li>
<li><a href="Reference/Global_Objects/Object/freeze" title="Object.freeze() 方法可以冻结一个对象,冻结指的是不能向这个对象添加新的属性,不能修改其已有属性的值,不能删除已有属性,以及不能修改该对象已有属性的可枚举性、可配置性、可写性。该方法返回被冻结的对象。"><code>Object.freeze()</code></a></li>
<li><a class="external" href="https://www.answers.com/Q/Which_animals_have_three_lungs" rel="noopener">"Which animals have three lungs?" on answers.com</a></li>
<li><a class="external" href="https://aliens.wikia.com/wiki/Klingon" rel="noopener">Klingons</a> (another answer to that query)</li>
</ul>
</article>