[undefined×2]是什么意思,为什么它与[undefined,undefined]不同? [英] What does [undefined × 2] mean and why is it different to [undefined,undefined]?

查看:166
本文介绍了[undefined×2]是什么意思,为什么它与[undefined,undefined]不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答这个问题时( [对象,对象]和数组(2))我曾经遇到过我以前没有意识到的JavaScript数组中的某些内容(具有讽刺意味的是,考虑到我在微软的Chakra引擎中使用Arrays)。

While answering this question ( Difference between [Object, Object] and Array(2) ) I happened across something in JavaScript arrays that I wasn't previously aware of (ironic, considering I worked on Arrays in the Chakra engine while at Microsoft).

如果我将其输入Chrome的JavaScript控制台......

If I enter this into Chrome's JavaScript console...

var x = Array(2);
var y = [undefined, undefined];
var z = [,];

...然后查询 x y z 变量我分别获得此输出:

...then when query the x, y and z variables I get this output respectively:

> x
< [undefined × 2]
<    length: 2

> y
< [undefined, undefined] (
<    0: undefined
<    1: undefined
<    length: 2

> z
< [undefined × 1]
<    length: 1

MDN的相关文档 数组(arrayLength) [] 文字语法说明:


如果传递给Array构造函数的唯一参数是一个整数,它返回一个新的JavaScript数组,其length属性设置为该数字(注意:这意味着一个 arrayLength 数组空插槽,不是具有实际未定义值的槽。)如果参数是任何其他数字,则抛出 RangeError 异常。

If the only argument passed to the Array constructor is an integer this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values). If the argument is any other number, a RangeError exception is thrown.

我注意到文档没有进一步阐述槽的概念 - 我假设它们正在使用它作为array el的同义词ement(我知道JavaScript数组可以是具有不同内部表示的稀疏数组,但这不应该影响消费者看到它们的方式 - 因为JavaScript数组和对象无论如何都是对各种数据结构的抽象)。

I note that the documentation does not further explicate on the concept of a "slot" - I assume they're using it as a synonym for "array element" (I'm aware that JavaScript arrays can be sparse-arrays that have a different internal representation, but that shouldn't affect how consumers see them - given that JavaScript arrays and objects are abstractions over a variety of data structures anyway).

MSDN上的相关文档没有提供 Array 函数的edge-case参数或Array-literal [] 语法。

The documentation on MSDN for the same does not provide any mention of edge-case arguments for the Array function or Array-literal [] syntax.

所以我的问题是:


  • undefined×2 是什么意思?

  • 为什么数组(2)评估为 [undefined×2] 而不是 [undefined,undefined]

  • 为什么 [,] 的长度为:1

  • What does undefined × 2 mean?
  • Why does Array(2) evaluate to [undefined × 2] instead of [undefined, undefined]?
  • Why does [,] have a length: 1?

为了回答我自己的问题,我咨询了关于Array Literals的ECMA-262 7.0规范。关于 elided 数组文字元素(强调我的)有一个有趣的注释:

To attempt to answer my own questions I consulted the ECMA-262 7.0 specification on Array Literals. There's an interesting note on elided array literal elements (emphasis mine):


数组元素可能会被删除元素列表的开头,中间或结尾。每当元素列表中的逗号前面没有AssignmentExpression(即开头的逗号或另一个逗号后面的逗号)时,缺少的数组元素会增加数组的长度并增加后续元素的索引。 未定义Elided数组元素。如果元素末尾的元素被省略,则该元素不会影响数组的长度。

所以粗体部分至少回答了我的第三个问题:如果最后一个元素是缺失那么它就不存在了,如果一个非终端元素没有值,那么它的 undefined 。所以这些表达式是等价的:

So the part in bold answers my third question, at least: if the last element is "missing" then it does not exist, and if a non-terminal element doesn't have a value then it's undefined. So these expressions are equivalent:

[a,]   --> [a]
[a,b,] --> [a,b]
[a,,b] --> [a,undefined,b]

因此

[,]    --> [undefined]
[,,]   --> [undefined,undefined]

所以我在Chrome中试过这个:

So I tried this in Chrome:

> var a = [,,];
> a
> [undefined × 2]
>    length: 2

这是出乎意料的!根据元素省略的明确规则,我认为 [,,] 相当于 [undefined,undefined] 但Chrome认为 [,,] 相当于 Array(2)(请注意,没有定义索引器,给它的结果与案例 y )相同。

This is unexpected! I thought [,,] to be equivalent to [undefined,undefined] as per the explicit rules of element elision, yet Chrome considers [,,] to be equivalent to Array(2) instead (note there are no indexers defined, giving it the same result as case y).

所以我的第四个问题:


  • 为什么 [,,] 相当于数组(2)但不是 [未定义,未定义]

  • Why is [,,] equivalent to Array(2) but not [undefined,undefined]?

推荐答案


undefined×2是什么意思?

What does undefined × 2 mean?

控制台显示一些开发人员的内容基于某些输入决定是一个有用的消息。它在任何意义上都不应被视为规范性的,在某些情况下会产生误导。声明:

Consoles display what some developer has decided is a useful message based on some input. It should not be regarded as normative in any sense, and in some cases is misleading. The statement:

var x = Array(2);

创建一个变量 x 并指定一个长度为2的新数组。在任何有意义的意义上都不是空位。它相当于:

Creates a variable x and assigns a new array with length 2. There are no "empty slots" in any meaningful sense. It is equivalent to:

var x = [,,];

(注意旧的IE有一个错误,上面创建了一个长度为3而不是2的数组)。您可以将其视为如下对象:

(noting that old IE had a bug where the above created an array with length 3 not 2). You can think of it as an object like:

var x = {length: 2};

可以添加的属性数量绝不受设置长度的限制。但是,设置现有数组的长度将删除索引等于或高于新长度的所有成员。

The number of properties that can be added is in no way limited by setting the length. However setting the length of an existing array will delete any members with indexes equal to or higher than the new length.

话虽如此,实现可以自由地为长度索引的价值,在某些情况下似乎是性能提升,但ECMA-262没有规定它。

Having said that, implementations are free to allocate space for "length" worth of indexes, which seems to be a performance boost in some cases, but it's not specified as being required by ECMA-262.


为什么Array(2)求值为[undefined×2]而不是[undefined,undefined]?

Why does Array(2) evaluate to [undefined × 2] instead of [undefined, undefined]?

见上文,控制台只是想要有所帮助。根本没有未定义的成员,只有一个长度为2的数组。对于记录,IE 10显示:

See above, the console is just trying to be helpful. There are no undefined members at all, there is just an array with length 2. For the record, IE 10 shows:

[undefined, undefined]

由于上述原因,这也具有误导性。它应该显示 [,,]

which is also misleading for the above reasons. It should show [,,].


为什么[,]有一个长度:1?

Why does [,] have a length: 1?

这被称为elision,它是 数组初始值设定项 或数组文字。它以与以下相同的方式创建缺失成员:

That is called an elision, which is part of an array initializer or array literal. It creates a "missing" member in the same way that:

var x = [];
x[0] = 0;
x[2] = 2;

在索引1没有成员,相当于:

does not have a member at index 1 and is equivalent to:

var x = [0,,2];

再次注意旧IE解释 [,] 长度为2.指数1的成员据说被省略。

Noting again that old IE interpreted [,] as having length of 2. The member at index 1 is said to be "elided".

这篇关于[undefined×2]是什么意思,为什么它与[undefined,undefined]不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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