Mat*_*att 65 javascript arrays jslint literals
JSLint给了我这个错误:
第11行第33个问题:使用数组文字符号[].
var myArray = new Array();
Run Code Online (Sandbox Code Playgroud)
什么是数组文字符号?为什么它要我使用它?
它显示在这里new Array();
应该工作正常......有什么我想念的吗?
Coo*_*une 94
array literal表示法是使用空括号定义新数组的位置.在你的例子中:
var myArray = [];
Run Code Online (Sandbox Code Playgroud)
它是定义数组的"新"方式,我认为它更短/更清晰.
以下示例解释了它们之间的区别:
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
Run Code Online (Sandbox Code Playgroud)
ken*_*ken 23
另请参见:var x = new Array()有什么问题;
除了Crockford论点之外,我认为这也是由于其他语言具有类似的数据结构,恰好使用相同的语法; 例如,Python有列表和词典 ; 请参阅以下示例:
// this is a Python list
a = [66.25, 333, 333, 1, 1234.5]
// this is a Python dictionary
tel = {'jack': 4098, 'sape': 4139}
Run Code Online (Sandbox Code Playgroud)
是不是它的Python如何在语法上正确Javascript?(是的,结束的分号丢失了,但Javascript也不需要那些)
因此,通过重复使用编程中的常见范例,我们可以避免每个人重新学习不应该的东西.