如果两者都调用构造函数“Array”并生成一个对象,有什么不同?
我知道如果我们创建一些没有 new
的对象,我们会丢失 this
:
function Animal(name) {this.name = name}
var duck = Animal('duck'); // undefined
但是 new Array(n)
和 Array(n)
是如何工作的呢?
最佳答案
没有区别。检查这个article :
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.
...............................
So the rule is simple: The only time we should use the new operator is to invoke a pseudoclassical Constructor function. When calling a Constructor function, the use of new is mandatory.
关于javascript - `new Array(n)` 和 `Array(n)` 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263876/