uTools-Manuals/docs/javascript/Reference/Errors/Non_configurable_array_element.html

54 lines
5.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: can't delete non-configurable array element (Firefox)
TypeError: Cannot delete property '2' of [object Array] (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/Reference/Global_Objects/Array/length#Shortening_an_array">缩短一个数组的长度</a>的时候,其中有元素是不可配置的(<a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">non-configurable</a>)。正常情况下,缩短数组的长度,则超出限度的元素会被删除,而这里指的是这种操作失效的情况。</p>
<p><code><font face="Open Sans, arial, x-locale-body, sans-serif"> </font>configurable</code>  特性控制着属性是否可以从对象中删除,以及它的特性(除了 writable 之外)是否可以发生改变。</p>
<p>通常,使用<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax">数组初始化语句</a>创建的对象是可配置的,而通过 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 创建的属性,默认则是不可配置的。</p>
<h2 id="示例">示例</h2>
<h3 id="通过_Object.defineProperty_创建的不可配置属性"><code><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif">通过 </font>Object.defineProperty 创建的不可配置属性</code></h3>
<p>使用 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 且在没有明确将属性设定为可配置的情况下,默认可以创建不可配置属性。</p>
<pre class="brush: js example-bad">var arr = [];
Object.defineProperty(arr, 0, {value: 0});
Object.defineProperty(arr, 1, {value: "1"});
arr.length = 1;
// TypeError: can't delete non-configurable array element
</pre>
<p>如果想要缩短数组长度的话,需要将其中的元素设置为可配置的。</p>
<pre class="brush: js example-good">var arr = [];
Object.defineProperty(arr, 0, {value: 0, configurable: true});
Object.defineProperty(arr, 1, {value: "1", configurable: true});
arr.length = 1;
</pre>
<h3 id="密封的数组"><code>密封的数组</code></h3>
<p> <a href="Reference/Global_Objects/Object/seal" title="The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>Object.seal()</code></a> 函数会将数组中现存的所有元素标记为不可配置。</p>
<pre class="brush: js example-bad">var arr = [1,2,3];
Object.seal(arr);
arr.length = 1;
// TypeError: can't delete non-configurable array element
</pre>
<p>(为了解决上述问题,)或者是移除 <a href="Reference/Global_Objects/Object/seal" title="The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>Object.seal()</code></a> 调用,或者将数组拷贝一份。在拷贝数组的情况下,缩短备份数组的长度并不会修改原始数组的长度。</p>
<pre class="brush: js example-good">var arr = [1,2,3];
Object.seal(arr);
// Copy the initial array to shorten the copy
var copy = Array.from(arr);
copy.length = 1;
// arr.length == 3
</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/Array/length" title="length 是Array的实例属性。返回或设置一个数组中的元素个数。该值是一个无符号 32-bit 整数,并且总是大于数组最高项的下标。"><code>Array.length</code></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/seal" title="The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>Object.seal()</code></a></li>
</ul>
</article>