10

Possible Duplicate:
creating objects - new object or object literal notation?

What exactly is the difference between the following:

var myData = new Object();
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";

and

var myData = {};
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";

Update

What I got is this...

var myData = {
    "name" : "ATOzTOA",
    "site" : "atoztoa",
};

is a shortcut to

var myData = new Object({
    "name" : "ATOzTOA",
    "site" : "atoztoa",
});

Am I right?

Community
  • 1
  • 1
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • http://stackoverflow.com/questions/4597926/creating-objects-new-object-or-object-literal-notation – Ofiris Dec 27 '12 at 08:53

3 Answers3

6

There is no difference (technically). {} is just a shortcut for new Object().

However, if you assign an object literal, you may directly form a new object with multiple properties.

var myData = {
    name:   'ATOzTOA',
    size:   'atoztoa'
};

..which might feel more convenient. Also, it reduces the access on the object and is ultimately faster. But that is about microoptimizations. More important is that its a lot less to type.

jAndy
  • 231,737
  • 57
  • 305
  • 359
3

Nothing. {} just a short hand for new Object()

Its same logic as your full name is 'Mark Zuckerberg' and people call you ' Hi Mark'

Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
0

No difference in my view , Both are initializing the object. nothing else , it is a shortcut.

Paritosh
  • 2,303
  • 2
  • 18
  • 24