In the first notation of the fooModel variable, an object was created without calling the constructor, in the second notation of fooModel , an object was created with the constructor fooModel as an anonymous function, therefore when using the constructor function of the keywords new , the object is called from its prototype (in this example, the prototype is not declared, therefore it is prototype standard object).
Conclusion
Use the second notation, if your object should call code when it is created, use first if not.
More on the second notation
The second notation also allows you to use local (private) variables and functions inside the constructor, because the constructor gives us our own scope.
var obj=new function(){ var priv="Local scope variable"; var method=function(){ console.log("Local method"); }; this.doStuff=function(){
The second notation with the constructor and new more often used with the standard declaration of the constructor function and the prototype declaration. This is the right way if we need to create more than one object. Methods and all common properties must not be declared in the prototype in the constructor.
var ExampleClass=function(bar){
Creating such an object:
var a=new ExampleClass("A bar"); //a.bar is "A bar" var b=new ExampleClass("B bar"); //b.bar is "B bar"
Objects a and b have the same prototype (it saves memory) , but they can have different properties specified in the constructor.
Offtop
There are so many possibilities for creating objects in javascript, I have a third example of how to run the code in the first notations:
window.fooModule = { init: function() { this.bar = "cee"; this.doStuff(); return this;
I create an object and run init at a time.