在JavaScript中设置数组长度是反模式吗? [英] Is it an antipattern to set an array length in JavaScript?

查看:143
本文介绍了在JavaScript中设置数组长度是反模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码是不是很糟糕:

Is it bad to use code like:

var a = [1,2,3,4];
a.length = 2; // 3 and 4 are removed

它是否有适当的浏览器支持?删除的值是否可以正确收集垃圾?

Does it have decent browser support? Do the removed values get garbage collected properly?

推荐答案


它是否具有良好的浏览器支持?

Does it have decent browser support?

是的。这是自第一版ECMAScript


具体来说,每当添加一个名称为数组索引的属性时,如果需要,
length属性更改为比该数组索引的数值多一个;每当
更改length属性时,将自动删除名称为数组索引且其值不小于
新长度的每个属性。

Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted.

标准ECMA-262,15.4

Standard ECMA-262, 15.4

在ECMAScript 5中,这被移到 15.4.5.2

In ECMAScript 5, this was moved to 15.4.5.2.


尝试将Array对象的length属性设置为一个值,该值在数值上小于或等于数组的现有数组索引不可删除属性的最大数字属性名称将导致长度设置为数字值大于该最大数字属性名称。

Attempting to set the length property of an Array object to a value that is numerically less than or equal to the largest numeric property name of an existing array indexed non-deletable property of the array will result in the length being set to a numeric value that is one greater than that largest numeric property name.

标准ECMA-262,15.4.5.2

Standard ECMA-262, 15.4.5.2

所有浏览器都支持此行为。

All browsers support this behavior.


删除值是否正确收集垃圾?

Do the removed values get garbage collected properly?

是的。 (参见上面的引文,并 15.4.5.1 。)

Yes. (See quotes above, and 15.4.5.1.)


在Javascript中设置数组长度是反模式吗?

Is it an antipattern to set array length in Javascript?

不,不是真的。虽然数组是ES6中的异国对象,但关于数组的真正疯狂的独特之处在于它们的索引,而不是设置 length 属性。您可以使用属性 setter <复制相同的行为/ a>。

No, not really. While arrays are "exotic objects" in ES6-speak, the real crazily unique thing about arrays are their indexing, not setting the length property. You could replicate the same behavior with a property setter.

在JS中,具有非微妙效果的属性设置器确实有点不寻常,因为它们的副作用可能不明显。但是由于 .length 已经从第0天开始使用Javascript,所以应该相当广泛地理解它。

It's true that property setters with non-subtle effects are somewhat unusual in JS, as their side-effects can be unobvious. But since .length has been there in Javascript from Day 0, it should be fairly widely understood.

如果你想要替代方案,请使用 .splice()

If you want an alternative, use .splice():

// a is the array and n is the eventual length

a.length = n;

a.splice(n, a.length - n); // equivalent
a.splice(n, a.length);     // also equivalent

如果我避免设置 .length 由于某种原因,这是因为它改变了数组,我更喜欢不可变的编程。不可变的替代方案是 .slice()

If I were avoid setting .length for some reason, it would be because it mutates the array, and I prefer immutable programming where reasonable. The immutable alternative is .slice().

a.slice(0, n);  // returns a new array with the first n elements

这篇关于在JavaScript中设置数组长度是反模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆