Javascript 在javascript中定义数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6047582/
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
defining array in javascript
提问by mcgrailm
I want to create an array in javascript and remember two ways of doing it so I just want to know what the fundamental differences are and if there is a performance difference in these two "styles"
我想在 javascript 中创建一个数组并记住两种方法,所以我只想知道根本区别是什么,以及这两种“样式”是否存在性能差异
var array_1 = new Array("fee","fie","foo","fum");
var array_2 = ['a','b','c'];
for (let i=0; i<array_1.length; i++){
console.log(array_1[i])
}
for (let i=0; i<array_2.length; i++){
console.log(array_2[i])
}
回答by T.J. Crowder
They do the same thing. Advantages to the []
notation are:
他们做同样的事情。[]
符号的优点是:
- It's shorter.
- If someone does something silly like redefine the
Array
symbol, it still works. - There's no ambiguity when you only define a single entry, whereas when you write
new Array(3)
, if you're used to seeing entries listed in the constructor, you could easily misread that to mean[3]
, when in fact it creates a new array with alength
of 3 and no entries. - It may be a tiny little bitfaster (depending on JavaScript implementation), because when you say
new Array
, the interpreter has to go look up theArray
symbol, which means traversing all entries in the scope chain until it gets to the global object and finds it, whereas with[]
it doesn't need to do that. The odds of that having any tangible real-world impactin normal use cases are low. Still, though...
- 它更短。
- 如果有人做了一些愚蠢的事情,比如重新定义
Array
符号,它仍然有效。 - 当您只定义一个条目时没有歧义,而当您编写时
new Array(3)
,如果您习惯于看到在构造函数中列出的条目,您很容易将其误读为意思[3]
,而实际上它创建了一个 alength
为 3的新数组,并且没有条目。 - 它可能会快一点(取决于 JavaScript 实现),因为当你说 时
new Array
,解释器必须去查找Array
符号,这意味着遍历作用域链中的所有条目,直到它到达全局对象并找到它,而有了[]
它不需要这样做。在正常用例中产生任何有形现实影响的可能性很低。不过,虽然...
So there are several good reasons to use []
.
所以有几个很好的理由使用[]
.
Advantages to new Array
:
优点new Array
:
- You can set the initial length of the array, e.g.,
var a = new Array(3);
- 您可以设置数组的初始长度,例如,
var a = new Array(3);
I haven't had any reason to do that in several years (not since learning that arrays aren't really arraysand there's no point trying to pre-allocate them). And if you really want to, you can always do this:
几年来我没有任何理由这样做(因为了解到数组不是真正的数组并且尝试预先分配它们没有意义)。如果你真的想要,你可以这样做:
var a = [];
a.length = 3;
回答by davin
There's no difference in your usage.
你的用法没有区别。
The only real usage difference is passing an integer parameter to new Array()
which will set an initial array length (which you can't do with the []
array-literal notation). But they create identical objects either way in your use case.
唯一真正的用法区别是传递一个整数参数,new Array()
该参数将设置初始数组长度(使用[]
数组文字表示法无法做到这一点)。但是它们在您的用例中以任何一种方式创建相同的对象。
回答by maerics
This benchmark on JSPerfshows the array literal form to be generally faster than the constructor on some browsers (and not slower on any).
JSPerf 上的这个基准测试显示数组文字形式通常比某些浏览器上的构造函数更快(并且在任何浏览器上都不慢)。
This behavior is, of course, totally implementation dependent, so you'll need to run your own test on your own target platforms.
当然,这种行为完全依赖于实现,因此您需要在自己的目标平台上运行自己的测试。
回答by troynt
I believe the performance benefits are negligible.
我相信性能优势可以忽略不计。
回答by Urjit
I think both ways are the same in terms of performance since they both create an "Array object" eventually. So once you start accessing the array the mechanism will be the same. I not too sure about how different the mechanisms to construct the arrays be (in terms of performance) though it shouldn't be any noticeable gains using one way to the other.
我认为这两种方式在性能方面是相同的,因为它们最终都创建了一个“数组对象”。因此,一旦您开始访问数组,机制将是相同的。我不太确定构造数组的机制有多大不同(在性能方面),尽管使用一种方式到另一种方式不应该有任何明显的收益。