两者基本相同,唯一不同点在于初始化:

var a = [],            // these are the same
b = new Array(),   // a and b are arrays with length 0

c = ['foo', 'bar'],           // these are the same
d = new Array('foo', 'bar'),  // c and d are arrays with 2 strings

// these are different:
e = [3]             // e.length == 1, e[0] == 3
f = new Array(3),   // f.length == 3, f[0] == undefined

也就是说Array(arg),其中的arg是指生成数组的长度。

参考:What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

 

检查array是否为空:

if (array === undefined || array.length == 0) {
    // array empty or does not exist
}

 

相关文章:

  • 2021-11-27
  • 2021-11-27
  • 2021-10-19
  • 2022-01-13
  • 2022-01-06
  • 2021-05-22
猜你喜欢
  • 2021-06-11
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案