JSLint 给我这个错误:
Problem at line 11 character 33: Use the array literal notation [].
var myArray = new Array();
什么是数组文字符号,为什么要我改用它?
此处显示 new Array();
应该可以正常工作...我是否遗漏了什么?
最佳答案
数组文字表示法是您仅使用空括号定义新数组的地方。在你的例子中:
var myArray = [];
这是定义数组的"new"方式,我想它更短/更清晰。
下面的例子解释了它们之间的区别:
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
Reference: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
关于javascript - 什么是 javascript 中的数组字面量表示法,什么时候应该使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1094723/