1

Possible Duplicate:
Javascript object creation using literals vs custom constructor functions
Function vs. Object Literal Notaion - Which is better, or is it a matter of preference?

Could anyone enlighten me what is the difference between below two ways of creating objects in JavaScript or more specifically drawbacks/advantages of them:

Function Foo() {
   function bar () {...}
}

And

var Foo = {
   bar : function() {}
}

Also since in latter way, there is no this keyword, how can I make it object instance member ?

3
  • Post some valid code - that first code sample is no good - and they do completely different things. I'm not sure how you want to compare them, since the first defines a function that can be used as constructor (with new) while the second is an object literal with a function. Anyways, you'll have this in the second bar function as well, not sure why you say that there isn't. Commented Jun 19, 2012 at 20:05
  • @Bergi, you're probably right about the dupes, but most answers there aren't very good or helpful IMHO. I'd say the second one has the best answers. Commented Jun 19, 2012 at 20:12
  • 2
    how can I make it object instance member What does this mean? Foo is already an object, an instance so to speak. Commented Jun 19, 2012 at 20:17

0