在js中,大家一般喜欢用的数组构造函数是new Array();
let cms=new Array(6,1,3,5);
console.log(cms.length);
console.table(cms);
咋一看,好像什么问题,当构造函数的参数为一个数字时:
let cms=new Array(6);
console.log(cms.length);
console.table(cms);
就会发现我们或许只是想构造一个只有一个数字的数组,但却构造了一个数组长度为6的数组,怎么解决这个问题呢?
Array.of()来帮你解决这个问题吧!
Array.of()和new Array()功能相同,但当只放入一个数字时,Array.of()会构造一个包含该数字的数组。
let arr=Array.of(6);
console.log(arr.length);
console.table(arr);`
```