The first is the return value of the function called immediately. The second function. In fact, it comes down to the fact that there is a difference between them:
var f = (function() { return 0; })(); var f = function() { return 0; };
Since the first function is called immediately, the value 0 is set by the variable f . The first f not a function. However, we must call the second f in order to get the value:
f();
It follows that in your example, the first BW.Timer is the object literal itself, and the second is a function that returns the object literal. You must call the function to go to the object:
BW.Timer().Add(x);
Why use the first one first?
You may ask yourself why instead of a = {} should use syntax like a = (function() { return {}; })() , but there is a good reason. IIFE (Immeditately-Invoked Function Expression), unlike a regular function, allows the emulation of static variables (variables that support their value through a single instance). For example:
var module = (function() { var x = 0; return { get: function() { return x }, set: function(n) { x = n } }; })();
The above is an example of a Module Pattern tutorial. Since the function is called immediately, the variable x is created, and the return value (object) is assigned to module . We cannot get to x except using the get and set methods provided for us. Therefore, x is static, that is, its variable will not be redefined every time you use module .
module.set(5); module.get();
On the other hand, look at an example where module declared as a function instead:
// assume module was made as a function module().set(5); module().get(); // 0
When we call module() , the variable x overridden every time. Therefore, we effectively use different instances of module and x each time we call module .