What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

后端 未结 18 1468
生来不讨喜
生来不讨喜 2020-11-21 12:00

What\'s the real difference between declaring an array like this:

var myArray = new Array();

and

var myArray = [];
<         


        
18条回答
  •  你的背包
    2020-11-21 12:53

    For more information, the following page describes why you never need to use new Array()

    You never need to use new Object() in JavaScript. Use the object literal {} instead. Similarly, don’t use new Array(), use the array literal [] instead. Arrays in JavaScript work nothing like the arrays in Java, and use of the Java-like syntax will confuse you.

    Do not use new Number, new String, or new Boolean. These forms produce unnecessary object wrappers. Just use simple literals instead.

    Also check out the comments - the new Array(length) form does not serve any useful purpose (at least in today's implementations of JavaScript).

提交回复
热议问题