javascript - Why do we need the "new" keyword when creating an array? - Stack Overflow

Maybe I'm asking this incorrectly, but I'm primarily wondering what is the difference between

Maybe I'm asking this incorrectly, but I'm primarily wondering what is the difference between creating an array with and without the "new" keyword? When is it appropriate to use?

var myPix = new Array("Images/pic1.jpg", "Images/pic2.jpg", "Images/pic3.jpg"); 

I know that the the "new" keyword creates a new object. But, since I'm creating a variable that holds the array object, isn't the array still created without using "new"?

Lastly, couldn't I just as easily use this?

var myPix = ["Images/pic1.jpg", "Images/pic2.jpg", "Images/pic3.jpg"];

Maybe I'm asking this incorrectly, but I'm primarily wondering what is the difference between creating an array with and without the "new" keyword? When is it appropriate to use?

var myPix = new Array("Images/pic1.jpg", "Images/pic2.jpg", "Images/pic3.jpg"); 

I know that the the "new" keyword creates a new object. But, since I'm creating a variable that holds the array object, isn't the array still created without using "new"?

Lastly, couldn't I just as easily use this?

var myPix = ["Images/pic1.jpg", "Images/pic2.jpg", "Images/pic3.jpg"];
Share Improve this question asked Oct 21, 2014 at 21:23 PadawanPadawan 7281 gold badge9 silver badges20 bronze badges 5
  • 5 "Lastly, couldn't I just as easily use this?" - yes, and you should. – David Thomas Commented Oct 21, 2014 at 21:25
  • Yes, in fact I advise people who ask for my advice to (almost) forget there's such a thing as the Array constructor. Using [ ] notation is less typing. – Pointy Commented Oct 21, 2014 at 21:25
  • 1 "Lastly, couldn't I just as easily use this?" Yup – j08691 Commented Oct 21, 2014 at 21:25
  • Creating a 'new Array()' is just the classc form of doing it in coding – Victor2748 Commented Oct 21, 2014 at 21:26
  • new is optional on native primitive constructors (String, Number, Object, RegExp, etc). except Date(), that thing is weird... – dandavis Commented Oct 21, 2014 at 21:28
Add a ment  | 

4 Answers 4

Reset to default 4

In Javascript, "classes" are really their own constructor/initializer functions. The Array function is an example. When you call new Array, Javascript first creates an uninitialized object, and then invokes Array() with that new object as this. If you run Array() without the new, you're skipping to that second step without creating any object to initialize.

With its built-in type functions like Array, Javascript will turn around and create the object for you even when called without new; they "do the right thing" because they're written that way. Your own type functions won't be so considerate unless you go out of your way to code them so.

In general, if you're looking for shorthand notation, you should just use the bracket form.

We don't need the new keyword for Array() because is has "exotic" behavior mandated by the ECMAScript standard:

The Array constructor:

...

  • creates and initializes a new Array when called as a constructor.
  • also creates and initializes a new Array when called as a function rather than as a constructor. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

You can do this either way.

In JavaScript, everything is an object including arrays and a typical way to instantiate is to use the keyword new. But, for a few cases including array, this can be avoided by declaring an array like this:

var arr = [3,45,43,4]; 

however, the second way (using []) of creating an array is preferred because:

  1. it is simpler
  2. it is faster

See discussion here - Why is arr = [] faster than arr = new Array?

using new Array("a", "b", "c") is equivalent to ["a", "b", "c"]. The latter is called Javascript Object Notation and is more script-like ala languages such as python. The returned object is still an Array in either case.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744986938a4604665.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信