I've seen this syntax for creating an anonymous constructor function in JavaScript:
var Application = Application || {};
!function(window, Application) {
Application.property = {/*...*/}
Application.method = function(){/*...*/}
}(window, Application);
I want to understand what the following parts here:
- What is the advantage of using first line (i.e.
var o = o || {};
) vs just statingvar o = (function(){})();
? - Why
!
is used in front of function? - Why would I pass
window
orApplication
as parameters when they are global object? - Is this the most convenient way for anonymous constructor function and how is this better than:
4a)
var Application = {
property: {},
method: function(){}
}
or 4b)
var Application = (function() {
var method = function(){/*...*/}
return {method:method};
}());