在 JavaScript 中声明数组的方式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11403068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the way of declaring an array in JavaScript?
提问by Celeritas
I'm just learning JavaScript and it seems like there are a number of ways to declare arrays.
我只是在学习 JavaScript,似乎有很多方法可以声明数组。
var myArray = new Array()
var myArray = new Array(3)
var myArray = ["apples", "bananas", "oranges"]
var myArray = [3]
var myArray = new Array()
var myArray = new Array(3)
var myArray = ["apples", "bananas", "oranges"]
var myArray = [3]
What are their difference, and what are the preferred ways?
它们有什么区别,首选的方式是什么?
According to this websitethe following two lines are very different:
根据该网站,以下两行非常不同:
var badArray = new Array(10); // creates an empty Array that's sized for 10 elements
var goodArray= [10]; // creates an Array with 10 as the first element
As you can see these two lines do two very different things. If you had wanted to add more than one item then badArray would be initialized correctly since Javascript would then be smart enough to know that you were initializing the array instead of stating how many elements you wanted to add.
正如你所看到的,这两行做了两件非常不同的事情。如果您想添加多个项目,那么 badArray 将被正确初始化,因为 Javascript 会足够聪明,知道您正在初始化数组,而不是说明您想要添加多少个元素。
Is what the authors trying to say is Array(10)
creates an array with precisely 10 elements and [10]
creates an array of undefined size with the 0th element being 10? Or what does this mean?
作者想说的是Array(10)
创建一个恰好包含 10 个元素[10]
的数组,并创建一个大小未定义的数组,其中第 0 个元素为 10?或者这是什么意思?
采纳答案by Rocket Hazmat
The preferred way is to always use the literal syntax with square brackets; its behaviour is predictable for any number of items, unlike Array
's. What's more, Array
is not a keyword, and although it is not a realistic situation, someone could easily overwrite it:
首选方法是始终使用带方括号的文字语法;与Array
's不同,它的行为对于任意数量的项目都是可预测的。更重要的Array
是,它不是关键字,虽然这不是现实情况,但有人可以轻松覆盖它:
function Array() { return []; }
alert(Array(1, 2, 3)); // An empty alert box
However, the larger issue is that of consistency. Someone refactoring code could come across this function:
然而,更大的问题是一致性问题。重构代码的人可能会遇到这个函数:
function fetchValue(n) {
var arr = new Array(1, 2, 3);
return arr[n];
}
As it turns out, only fetchValue(0)
is ever needed, so the programmer drops the other elements and breaks the code, because it now returns undefined
:
事实证明, onlyfetchValue(0)
是永远需要的,所以程序员删除了其他元素并破坏了代码,因为它现在返回undefined
:
var arr = new Array(1);
回答by Rocket Hazmat
In your first example, you are making a blank array, same as doing var x = []
. The 2nd example makes an array of size 3 (with all elements undefined
). The 3rd and 4th examples are the same, they both make arrays with those elements.
在您的第一个示例中,您正在制作一个空白数组,与执行var x = []
. 第二个示例创建了一个大小为 3 的数组(包含所有元素undefined
)。第三个和第四个例子是一样的,它们都用这些元素组成数组。
Be careful when using new Array()
.
使用时要小心new Array()
。
var x = new Array(10); // array of size 10, all elements undefined
var y = new Array(10, 5); // array of size 2: [10, 5]
The preferred way is using the []
syntax.
首选方法是使用[]
语法。
var x = []; // array of size 0
var y = [10] // array of size 1: [1]
var z = []; // array of size 0
z[2] = 12; // z is now size 3: [undefined, undefined, 12]
回答by Deepak
There are a number of ways to create arrays.
有多种方法可以创建数组。
The traditional way of declaring and initializing an array looks like this:
声明和初始化数组的传统方式如下所示:
var a = new Array(5); // declare an array "a", of size 5
a = [0, 0, 0, 0, 0]; // initialize each of the array's elements to 0
Or...
或者...
// declare and initialize an array in a single statement
var a = new Array(0, 0, 0, 0, 0);
回答by Zack
To declare it:
声明它:
var myArr = ["apples", "oranges", "bananas"];
To use it:
要使用它:
document.write("In my shopping basket I have " + myArr[0] + ", " + myArr[1] + ", and " + myArr[2]);
回答by kennebec
If you are creating an array whose main feature is it's length, rather than the value of each index, defining an array as var a=Array(length);
is appropriate.
如果您正在创建一个数组,其主要特征是它的长度,而不是每个索引的值,那么定义一个数组var a=Array(length);
是合适的。
eg-
例如-
String.prototype.repeat= function(n){
n= n || 1;
return Array(n+1).join(this);
}