美文网首页优美编程
关于Array(10).fill

关于Array(10).fill

作者: 小遁哥 | 来源:发表于2019-02-12 19:09 被阅读2次

Array(10) 和 new Array(10)的区别

从语义上来看,前者是将Array当作普通方法来调用,后者是将Array当作构造方法来调用。

当参数一样时,两者并无区别

写代码时用第一种就好。

下面是EMAScript的标准

The Array constructor is the %Array% intrinsic object and the initial value of the Array property of the global object. When called as a constructor it creates and initializes a new exotic Array object. When Array is called as a function rather than as a constructor, it also creates and initializes a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

Array(10)生成了什么

Array(10) 返回的是只有length属性的数组。


image.png

empty 也可以通过字面量的方式生成

var list = [1,,2];

当调用fill时,不穿参数,则填充undefined

Array(10) 和高阶函数

以forEach、some、every、map、find、reduce为例,主要看当元素为empty、undefined、null时是否会触发回调

empty 只会触发find,empty被当作undefined处理;every、reduce时,empty对最终结果无影响。

undefined和null都出触发,every、reduce时对最终结果有影响

Array(10) 与for...in 和for... of

for... in 不会遍历 empty,但不会影响后面元素的遍历
for...of 会遍历empty,当作undefined处理

相关文章

网友评论

    本文标题:关于Array(10).fill

    本文链接:https://www.haomeiwen.com/subject/cfbkeqtx.html