javascript - What the different between `new Array(n)` and `Array(n)` -
what different if both of them call constructor "array" , generate object?
i know lost this
if create object without new
:
function animal(name) {this.name = name} var duck = animal('duck'); // undefined
but how works new array(n)
, array(n)
?
there no difference. check article:
you never need use new object() in javascript. use object literal {} instead. similarly, don’t use new array(), use array literal [] instead. arrays in javascript work nothing arrays in java, , use of java-like syntax confuse you.
do not use new number, new string, or new boolean. these forms produce unnecessary object wrappers. use simple literals instead.
...............................
so rule simple: time should use new operator invoke pseudoclassical constructor function. when calling constructor function, use of new mandatory.
Comments
Post a Comment