tags:

views:

2466

answers:

6

There are two different ways to create an empty object in JavaScript:

var objectA = {}
var objectB = new Object()

Is there any difference in how the script engine handles them? Is there any reason to use one over the other?

Similarly it is also possible to create an empty array using different syntax:

var arrayA = []
var arrayB = new Array()
+4  A: 

This is essentially the same thing. Use whatever you find more convenient.

Tomalak
+7  A: 

These have the same end result, but I would simply add that using the literal syntax can help one become accustomed to the syntax of JSON (a string-ified subset of JavaScript literal object syntax), so it might be a good practice to get into.

One other thing: you might have subtle errors if you forget to use the new operator. So, using literals will help you avoid that problem.

Ultimately, it will depend on the situation as well as preference.

Jason Bunting
+3  A: 
var objectA = {}

is a lot quicker and, in my experience, more commonly used, so it's probably best to adopt the 'standard' and save some typing.

Bobby Jack
+2  A: 

The object and array literal syntax {}/[] was introduced in JavaScript 1.2, so is not available (and will produce a syntax error) in versions of Netscape Navigator prior to 4.0.

My fingers still default to saying new Array(), but I am a very old man. Thankfully Netscape 3 is not a browser many people ever have to consider today...

bobince
Netscape 3? Man, that was in the previous *century*! :-D
Tomalak
A: 

I believe {} was recommended in one of the Javascript vids on here as a good coding convention. new is necessary for pseudoclassical inheritance. the var obj = {}; way helps to remind you that this is not a classical object oriented language but a prototypal one. Thus the only time you would really need new is when you are using constructors functions. For example:

var Mammal = function (name) {
  this.name = name;
};

Mammal.prototype.get_name = function () {
  return this.name;
}

Mammal.prototype.says = function() {
  return this.saying || '';
}

Then it is used like so:

var aMammal = new Mammal('Me warm-blooded');
var name = aMammal.get_name();

Another advantage to using {} as oppose to new Object is you can use it to do JSON-style object literals.

Thedric Walker
+13  A: 

Objects

There is no benefit to using new Object(); -- whereas {}; offers a few.

For defining empty objects they're technically the same. The {} syntax is shorter, neater (less Java-ish), and allows you to instantly populate the object inline - like so:

var myObject = {
    title:  'Frog',
    url:    '/img/picture.jpg',
    width:  300,
    height: 200
};

...which usually makes your code more compact, and more readable.

Arrays

For arrays, there's similarly almost no benefit to ever using new Array(); over []; -- with one minor exception:

var emptyArray = new Array(100);

creates a 100 item long array with all slots containing undefined -- which may be nice/useful in certain situations.

My recommendation:

  1. Never use new Object(); -- it's klunky and looks silly.
  2. Always use []; -- except when you need to quickly create an "empty" array with a predefined length.
Már Örlygsson
Even if you use the Array(100) syntax, that same array, at the 101st position has undefined in it; the only thing that number really does is change the value of the length property.
Jason Bunting
Yes. new Array(100); is a nice one-liner way to do that. ....ah... I see that I'd accidentally left in the word "important" ... will edit that out.
Már Örlygsson