【发布时间】:2017-02-07 23:39:08
【问题描述】:
[,'a'].every(x => x==='a')
> True
返回 True,虽然第一项是“未定义”,因此应该是 False?
[,'a'].map(x => x)
> [undefined × 1, "a"]
【问题讨论】:
标签: javascript arrays ecmascript-6
[,'a'].every(x => x==='a')
> True
返回 True,虽然第一项是“未定义”,因此应该是 False?
[,'a'].map(x => x)
> [undefined × 1, "a"]
【问题讨论】:
标签: javascript arrays ecmascript-6
内置数组方法忽略 sparse arrays 上不存在的属性。第一项不是undefined,索引0 中根本没有属性。你可以试试
[,'a'].every(x => x==='a')
> true
[undefined,'a'].every(x => x==='a')
> false
【讨论】:
Object.keys([,'a']) 返回["1"],而不是["0","1"]。
由于 undefined 是一个 typeof,所以使用 ==(两次)而不是三次。它返回 true 因为 x 是空的,因此 typeof undefined 匹配 undefined (因为你使用 = 三次)
【讨论】:
typeof 在哪里发挥作用?