JavaScript 声明数组

声明数组

1.数组字面量

let arr1 = []    //[]
let arr2 = [1,2,3]  //[1,2,3]

2.Array 构造函数

let arr3 = new Array()   //[]
let arr4 = new Array(3)   //[,,]
let arr5 = new Array(1,2,3)   //[1,2,3]
let arr5 = new Array(3).fill(true)   //[true,true,true]
  • 如果参数传一个正整数n,则会创建一个长度为n的空数组
  • 如果不是上一个情况,则传的任意个参数,将按顺序成为返回数组中的元素

3.Array.of()

创建一个具有可变数量参数的新数组实例

  • 任意个参数,将按顺序成为返回数组中的元素
let arr6 = Array.of()  //[]
let arr6 = Array.of(3)  //[3]
let arr6 = Array.of(1,2,3)  //[1,2,3]

猜你喜欢

转载自blog.csdn.net/qq_39706777/article/details/120311121