36 lines
3.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 class="syntaxbox">TypeError: property "x" is non-configurable and can't be deleted. (Firefox)
TypeError: Cannot delete property 'x' of #&lt;Object&gt; (Chrome)
</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>属性控制是否该属性能从对象上删除,以及它的属性(除了<code>writable</code>)能否被修改。</p>
<p>这个错误仅仅在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下出现。在非严格模式下,这个操作返回 <code>false</code></p>
<h2 id="示例">示例</h2>
<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 class="brush: js example-bad">'use strict';
var obj = Object.freeze({name: 'Elsa', score: 157});
delete obj.score; // TypeError
'use strict';
var obj = {};
Object.defineProperty(obj, 'foo', {value: 2, configurable: false});
delete obj.foo; // TypeError
'use strict';
var frozenArray = Object.freeze([0, 1, 2]);
frozenArray.pop(); // TypeError
</pre>
<p>也有一些内建于 JavaScript 的不可配置属性。你可能会尝试删除一个数学常量。</p>
<pre class="brush: js example-bad">'use strict';
delete Math.PI; // TypeError</pre>
<h2 id="另见">另见</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete operator</a></li>
<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>
</ul>
</article>