2019-04-21 11:50:48 +08:00

61 lines
3.5 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: setting getter-only property "x" (Firefox)
TypeError: Cannot set property "prop" of #&lt;Object&gt; which has only a getter (Chrome)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p>仅在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下报 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> 错误。</p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>该错误提示出现于试图给一个仅仅设置了 <a href="/en-US/docs/Web/JavaScript/Reference/Functions/get">getter</a> 特性的属性赋新值的时候。在非严格模式下会被静默忽略,而在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下会报 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> 错误。</p>
<h2 id="示例">示例</h2>
<p>下面的例子展示了如何为一个属性设置 getter 特性。由于没有设置 <a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">setter</a> 特性,所以在试图将 temperature 属性值设置为 30 的时候会报 TypeError 的错误。相关细节信息可以参考 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 页面。</p>
<pre><code class="language-js example-bad">"use strict";
function Archiver() {
var temperature = null;
Object.defineProperty(this, 'temperature', {
get: function() {
console.log('get!');
return temperature;
}
});
}
var arc = new Archiver();
arc.temperature; // 'get!'
arc.temperature = 30;
// TypeError: setting getter-only property "temperature"</code></pre>
<p>至于修复问题的方法,可以将第 16 行的代码移除,因为它试图为 temperature 属性赋值,或者是为它添加一个 <a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">setter</a> 特性,就像下面这样:</p>
<pre><code class="language-js example-good highlight[12]">"use strict";
function Archiver() {
var temperature = null;
var archive = [];
Object.defineProperty(this, 'temperature', {
get: function() {
console.log('get!');
return temperature;
},
set: function(value) {
temperature = value;
archive.push({ val: temperature });
}
});
this.getArchive = function() { return archive; };
}
var arc = new Archiver();
arc.temperature; // 'get!'
arc.temperature = 11;
arc.temperature = 13;
arc.getArchive(); // [{ val: 11 }, { val: 13 }]</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/defineProperties" title="Object.defineProperties() 方法直接在一个对象上定义新的属性或修改现有属性,并返回该对象。"><code>Object.defineProperties()</code></a></li>
</ul>
</article>