what is the difference between new object and object literal notation

what is the difference between new object and object literal notation  using -'javascript,object,jslint'

What is the difference between this constructor-based syntax for creating an object:

person = new Object()

...and this literal syntax:

person = {
    property1 : "Hello"

It appears that both do the same thing, although JSLint prefers you use object literal notation.

Which one is better and why?

asked Sep 14, 2015 by SteGERY
0 votes

6 Answers

0 votes

They both do the same thing, but literal notation takes less space. It's clearly recognizable as to what is happening, so using new object(), you are really just typing more.


person = new Object() /*You should put a semicolon here too.  
It's not required, but it is good practice.*/ 

person = {
    property1 : "Hello"

technically do not do the same thing. The first just creates an object. The second creates one and assigns a property. For the first one to be the same you then need a second step to create and assign the property.

answered Sep 14, 2015 by CindiW39vgzp
0 votes

There is no difference for a simple object without methods as in your example. However, there is a big difference when you start adding methods to your object.

Literal way :

function Obj( prop ) { 
 return { 
 p : prop, 
 sayHello : function(){ alert(this.p); }, 

Prototype way :

function Obj( prop ) { 
 this.p = prop; 
Obj.prototype.sayHello = function(){alert(this.p);}; 

With both of these code, you can create instances of Obj like this:

var foo = new Obj( "hello" ); 

However, with the literal way you carry a copy of the sayHello method within each instance of your objects, whereas with the prototype way the method is defined in the object prototype and shared between all object instances. If you have a lot of objects and/or a lot of methods, the literal way can lead to quite big memory waste.

answered Sep 14, 2015 by WBNSpencerg
0 votes

In JavaScript, we can declare a new empty object in two ways:

var obj1 = new Object();  
var obj2 = {};  

I have found nothing to suggest that there is any significant difference these two with regard to how they operate behind the scenes (please correct me if i am wrong – I would love to know). However, the second method (using the object literal notation) offers a few advantages.

  1. It is shorter (10 characters to be precise)
  2. It is easier, and more structured to create objects on the fly
  3. It doesn’t matter if some buffoon has inadvertently overridden Object

Consider a new object that contains the members Name and TelNo. Using the new Object() convention, we can create it like this:

var obj1 = new Object();  
obj1.Name = "A Person";  
obj1.TelNo = "12345"; 

The Expando Properties feature of JavaScript allows us to create new members this way on the fly, and we achieve what were intending. However, this way isn’t very structured or encapsulated. What if we wanted to specify the members upon creation, without having to rely on expando properties and assignment post-creation?

This is where the object literal notation can help:

var obj1 = {Name:"A Person",TelNo="12345"};  

Here we have achieved the same effect in one line of code and significantly fewer characters.

A further discussion the object construction methods above can be found at: JavaScript and Object Oriented Programming (OOP).

And finally, what of the idiot who overrode Object? Did you think it wasn’t possible? Well, this JSFiddle proves otherwise. Using the object literal notation prevents us from falling foul of this buffoonery.

(From http://www.jameswiseman.com/blog/2011/01/19/jslint-messages-use-the-object-literal-notation/)

answered Sep 14, 2015 by Can64R
0 votes

On my machine using nodejs I ran the following:

console.log('Testing Array:');
for(var i=0; i<200000000; i++){var arr = []};

console.time('using new');
for(var i=0; i<200000000; i++){var arr = new Array};
console.timeEnd('using new');

console.log('Testing Object:');

for(var i=0; i<200000000; i++){var obj = {}};

console.time('using new');
for(var i=0; i<200000000; i++){var obj = new Object};
console.timeEnd('using new');

note, this is an extension of what is found here: Why is arr = [] faster than arr = new Array?

my output was the following:

Testing Array:
using[]: 1091ms
using new: 2286ms
Testing Object:
using{}: 870ms
using new: 5637ms

so clearly {} and [] are faster than using new for creating empty objects/arrays.

answered Sep 14, 2015 by TrentGambrel
0 votes

Everyone here is talking about the similarities of the two. I am gonna point out the differences.

  1. Using new Object() allows you to pass another object. The obvious outcome is that the newly created object will be set to the same reference. Here is a sample code:

    var obj1 = new Object();
    obj1.a = 1;
    var obj2 = new Object(obj1);
    obj2.a // 1
  2. The usage is not limited to objects as in OOP objects. Other types could be passed to it too. The function will set the type accordingly. For example if we pass integer 1 to it, an object of type number will be created for us.

    var obj = new Object(1);
    typeof obj // "number"
  3. The object created using the above method (new Object(1)) would be converted to object type if a property is added to it.

    var obj = new Object(1);
    typeof obj // "number"
    obj.a = 2;
    typeof obj // "object"
  4. If the object is a copy of a child class of object, we could add the property without the type conversion.

    var obj = new Object("foo");
    typeof obj // "object"
    obj === "foo" // true
    obj.a = 1;
    obj === "foo" // true
    obj.a // 1
    var str = "foo";
    str.a = 1;
    str.a // undefined
answered Sep 14, 2015 by LouGoforth
0 votes

Also, according to some of the O'Really javascript books....(quoted)

Another reason for using literals as opposed to the Object constructor is that there is no scope resolution. Because it’s possible that you have created a local constructor with the same name, the interpreter needs to look up the scope chain from the place you are calling Object() all the way up until it finds the global Object constructor.

answered Sep 14, 2015 by LorFugate