admin管理员组文章数量:1287940
I remember reading somewhere (I think it was in one of Crockford's papers) that using an array literal []
is better than using the new Array();
notation.
But I can't really remember any advantages of one over the other.
Can anyone please explain to me on why the former is preferred over the latter?
Here is one reason I can think of on why []
is better than new Array();
:
var Array = function () { };
Overriding the Array
object will break code...!
Any more reasons?
I remember reading somewhere (I think it was in one of Crockford's papers) that using an array literal []
is better than using the new Array();
notation.
But I can't really remember any advantages of one over the other.
Can anyone please explain to me on why the former is preferred over the latter?
Here is one reason I can think of on why []
is better than new Array();
:
var Array = function () { };
Overriding the Array
object will break code...!
Any more reasons?
Share Improve this question edited Apr 29, 2018 at 12:56 Donald Duck 8,87323 gold badges79 silver badges102 bronze badges asked Nov 25, 2009 at 23:13 Andreas GrechAndreas Grech 108k101 gold badges303 silver badges362 bronze badges 2- 2 of course overriding the array object will break code. If you shoot yourself in the foot, you also might not be able to walk any more. – nickf Commented Nov 25, 2009 at 23:38
- 1 hehe I know I know...but what if some (dumb) library or maybe some stupid plugin overrides it heh? – Andreas Grech Commented Nov 25, 2009 at 23:39
4 Answers
Reset to default 37Brevity
It has less bytes to transfer over the wire, less bytes to interpret, less mental resources to parse it.
Less is more.
Consistency
What is the difference between these two lines of code?
var arr = [5];
var arr = new Array(5);
According to here new Array(5);
will not return an array with a 5 in it, instead it will return a 5 element array, with all the elements being undefined
. Whereas these two lines return identical arrays.
var arr = [5,6];
var arr = new Array(5,6);
One good reason is because the Array constructor has completely non-intuitive behavior. For example:
var a = new Array(5);
console.log(a.length); //prints "5"
console.log(a[0]); //prints "undefined"
var b = new Array(1,2);
console.log(b.length); //prints "2"
console.log(b[0]); //prints "1"
In this case, a ends up being an array of size 5 with all elements undefined, and b ends up being an array of size 2, with the values [1,2].
var c = new Array("5");
console.log(c.length); //prints "1"
console.log(c[0]); //prints "5"
And here, you end up with a single-element array, containing "5"
In general, you should probably never use the constructors on built-in types in Javascript. They all have weird edge cases like this. For example:
var s = new String("Hello");
var l = "Hello";
console.log(typeof(s)); // prints "object"
console.log(typeof(l)); // prints "string"
Elegance
To put it simply it just looks nicer AND is easier for a programmer to parse, especially for more complicated data structures:
var a = [
['a list', 'of', 'strings'],
[1, 2, 3, 4],
{ hello: "This is an object property." }
];
Versus the clumsier OOP-oriented method:
var a = new Array();
var a2 = new Array();
a2.push('a list');
a2.push('of');
a2.push('strings');
var a3 = new Array();
a3.push(1);
a3.push(2);
a3.push(3);
a3.push(4);
var o = new Object();
o.hello = "This is an object property";
a.push(a2, a3, o);
new Array(1, 2, 3)
instead of [1, 2, 3]
is the same as new String("cow")
instead of "cow"
本文标签: In JavaScriptwhy is preferred over new Array()Stack Overflow
版权声明:本文标题:In JavaScript, why is [ ] preferred over new Array();? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737370460a1985241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论