什么是JavaScript数组的文字符号,你应该何时使用它? [英] What is array literal notation in javascript and when should you use it?
问题描述
JSLint的是给我这个错误:
问题在第11行33字:使用数组文字符号[]
块引用>myarray的无功=新的Array();
什么是数组文字符号和它为什么要我用它代替?
在这里显示,
?新的Array();
应做工精细...有什么我失踪解决方案阵列文字符号是你定义只使用空方括号的新数组。在您的例子:
myarray的无功= [];
这是定义数组的新的方式,我想这是更短/清洁剂。
下面的例子说明它们之间的区别:
VAR A = [],//这是相同的
B =新阵列(),// a和b是长度为0的数组 C = ['富','酒吧'],//这是相同的
D =新的Array('富','棒'),// C和D是具有2个字符串数组 //这些是不同的:
E = [3],// e.length == 1,E [0] == 3
F =新阵列(3); // f.length == 3,F [0] ==未定义
参考:什么是阵列()和[]之间的差异,同时宣布一个JavaScript数组?
块引用>JSLint is giving me this error:
Problem at line 11 character 33: Use the array literal notation [].
var myArray = new Array();
What is array literal notation and why does it want me to use it instead?
It shows here that
new Array();
should work fine... is there something I'm missing?解决方案array literal notation is where you define a new array using just empty brackets. In your example:
var myArray = [];
It is the "new" way of defining arrays, and I suppose it is shorter/cleaner.
The examples below explain the difference between them:
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数组的文字符号,你应该何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!