uTools-Manuals/docs/javascript/Reference/Errors/Cant_redefine_property.html
2019-04-21 11:50:48 +08:00

29 lines
2.8 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: can't redefine non-configurable property "x" (Firefox)
TypeError: Cannot redefine property: "x" (Chrome)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>这种错误的起因在于试图给对象重新定义一个属性,但是该属性是<a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">不可配置的</a>。 <code>configurable</code> 特性控制着该属性是否可以从对象中删除,以及它的各个特性(除 writable 之外)是否可以修改。通常使用<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">对象初始化语句</a>定义的对象属性是可配置的。而使用 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 定义的属性则默认不可配置。</p>
<h2 id="示例">示例</h2>
<h3 id="使用_object.defineProperty_创建的不可配置属性">使用 object.defineProperty 创建的不可配置属性</h3>
<p>在使用 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 创建属性的时候,如果没有明确将其设定为可配置的,那么创建出来的属性就是不可配置的。</p>
<pre><code class="language-js example-bad">var obj = Object.create({});
Object.defineProperty(obj, "foo", {value: "bar"});
Object.defineProperty(obj, "foo", {value: "baz"});
// TypeError: can't redefine non-configurable property "foo"
</code></pre>
<p>如果想要稍后重新定义的话,那么需要将 "foo" 属性设置为可配置的。</p>
<pre><code class="language-js example-good">var obj = Object.create({});
Object.defineProperty(obj, "foo", {value: "bar", configurable: true});
Object.defineProperty(obj, "foo", {value: "baz", configurable: true});</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">[[Configurable]]</a></li>
<li><a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a></li>
</ul>
</article>