Javascript 奇怪的ES6数组。每个行为
返回True,尽管第一项为“未定义”,因此应为False?Javascript 奇怪的ES6数组。每个行为,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,返回True,尽管第一项为“未定义”,因此应为False? [,'a'].every(x => x==='a') > True 由于undefined是typeof,因此使用==(两次)而不是三次。它返回true,因为x为空,因此typeof undefined与undefined匹配(因为使用=三次)因为undefined是typeof,所以使用==(两次)而不是三次。它返回true,因为x为空,因此typeof undefined与undefined匹配(因为您使用了=三次)
[,'a'].every(x => x==='a')
> True
由于undefined是typeof,因此使用==(两次)而不是三次。它返回true,因为x为空,因此typeof undefined与undefined匹配(因为使用=三次)因为undefined是typeof,所以使用==(两次)而不是三次。它返回true,因为x为空,因此typeof undefined与undefined匹配(因为您使用了=三次)内置数组方法会忽略上不存在的属性。第一项不是未定义的,索引
0
中根本没有属性。你可以试试
[,'a'].map(x => x)
> [undefined × 1, "a"]
内置数组方法忽略上不存在的属性。第一项不是未定义的,索引
0
中根本没有属性。你可以试试
[,'a'].map(x => x)
> [undefined × 1, "a"]
这是同样的事实
typeof
在哪里起作用?这是同样的事实typeof
在哪里起作用?证明是稀疏数组:对象。键([,'a'])
返回[“1”]
,而不是[“0”,“1”]
。证明是稀疏数组:对象。键([,'a'])
返回[“1”
,而不是[“0”,“1”]
。