1

What is the difference between

var module = (function(){
    return {} 
})()

and

(function(context){
    var module = {}
    context.module = module;
})(this)
2
  • If this is in global scope, they do exactly the same thing. Commented Aug 26, 2012 at 17:49
  • Another one that I came up with a few months ago is var module = new function() { /* use this as the module */ }. Commented Aug 26, 2012 at 17:49

2 Answers 2

2

A property of this is not equivalent to a variable. In the global scope (i.e. where this references window), they are similiar. Yet, for example they will have a different behavior when you try to delete them:

> this.module = {};
> delete this.module
true
> var module = {};
// cant be deleted

Apart from that, both snippets create an empty object, wrapped inside a closure where you could define local (private) variables/functions etc. In your second function the object is also assigned to the local variable module, but that could be done in the first one as well.


@Eric: Your approach, using the new operator, is similiar to the first one regarding the variable. However, it will create an instance of that anonymous function instead of returning a plain object. It will inherit properties from a custom prototype, so for example module.constructor will then point to the anonymous function (which cannot be garbage collected, but e.g. even reused to create a clone). I would not recommend to use this.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 (I didn't not know var had that affect in the global scope; I thought it was simply "ignored". Is this for ECMAScript 3rd edition as well?)
Uh, that was on a comment by @Eric. I thought it would be the OP's one
-1

Top one is revealing module pattern. It lets you define private functions (as much as you can in javascript) and choose which ones are made public via the return call.

var module = (function(){ 
    function foo() {}; // Public, since it's returned

    function bar() {}; // Private, since it is not returned

    return {
        foo: foo
    } 
})();

The bottom one, as far as I can tell, just assigns an object literal to another object's namespace. It's probably the start of a singelton pattern.

(function(context){
    var module = {};
    context.module = module;
}(this)

The module could actually be defined with the revealing module pattern, hard to say since in your example it's just an object literal.

7 Comments

In the second one you also can have private stuff and choose which to expose then.
@MikeRobinson: Both patterns create singleton modules. There is no real difference in that aspect
This is not true. They are effectively the same in this aspect (of new scope and visibility patterns). The first simply assigns to window.module (a property assignment to module in the global scope) while the 2nd implicitly assigns to window.module through context.module through this (evaluates to window when called).
Which leads me to the next question. Also just wondering. Is there a pattern to create several instances of a module. Or what approach is suitable for that case?
@monkee Generally a "module" is a singleton with a "stable well-known name". Whereas objects from constructors/prototypes (or "classes") are instantiated one or more times .. of course, since a "module" is an object (with a "stable well-known name") ..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.