0

In javascript we can define an object prototype in various way.

First one

var foo={
a:1,
b:2
}

Second is

var foo=function(){
this.a=1;
this.b=2;
}

what i know is that the first on is an object itself while for creating an object from second definition we have to use new foo(). is there any other differences between these two definition. and if both are same then what is a need of second if not then what is need of first?

Nitin9791
  • 1,124
  • 1
  • 14
  • 17
  • Related: [What is the 'new' keyword in JavaScript?](http://stackoverflow.com/q/1646698/710446) – apsillers Dec 28 '15 at 20:50
  • There's more ways, like [Object.create](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create). – Teemu Dec 28 '15 at 20:56

1 Answers1

1

They are not the same, the second is a constructor for the Object:

var foo = {
  a: 1,
  b: 2
}
console.log("First: ",
    typeof foo, // object
    JSON.stringify(foo), // {a:1,b:2}
    foo.constructor) // function Object() { [native code] }

var foo = function() {
  this.a = 1;
  this.b = 2;
}
console.log("Second: ",
    typeof foo, // function
    JSON.stringify(foo), // undefined, foo is not an Object, it's a function!
    foo.constructor) // function Object() { [native code] }

var foo = new foo()
console.log("Thrid: ",
    typeof foo, // object
    JSON.stringify(foo), // {a:1,b:2}
    foo.constructor) // function() {  this.a = 1;  this.b = 2;}

So even after creating both object you will have a diffrence in their .constructor attribute. As the first Object was created by the Native constructor vor Object in JavaScript, and the second was created by your custom constructor.

But thats the only diffrence

CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • i am not saying they are same but once you did new foo() for second definition you get an object . these are various ways to define object's behaviors and property. i want to know what is the difference between them why we need second way of object definition. – Nitin9791 Dec 28 '15 at 20:54
  • 1
    @Nitin9791 see the explanation after the Code :) – CoderPi Dec 28 '15 at 20:55
  • 1
    "*...a difference in their `.constructor` attribute.*" -- More importantly, there will be a difference in the objects' prototype chains! (e.g., `foo.prototype.bar = 6` will cause `bar` to be an inherited property on all `foo` instances) (@Nitin9791) – apsillers Dec 28 '15 at 20:57
  • fine but if both are different then what is the need of first? Is it only to make sinleton object. – Nitin9791 Dec 28 '15 at 21:03
  • The need for first is the advantage that you don't need a constructor function for it? – CoderPi Dec 28 '15 at 21:05
  • @Nitin9791 Why do we have a division operator when we can multiply by values less than `1`? Different tools for different goals. `:)` Does the [linked duplicate](http://stackoverflow.com/questions/4859800/should-i-be-using-object-literals-or-constructor-functions) answer your question about when it's best to use each one? – apsillers Dec 28 '15 at 21:07
  • @apsillers yes i read that link but one thing which is still in my mind is that are the object literals only used for singleton object or they have some extra advantage which is not possible by the constructor definition? – Nitin9791 Dec 28 '15 at 21:16