1

I see a lot of code here, here:

var module = (function (module) {
    module.prototype.something_else = function () {
    };
    return module;
})(module);

Why var module = part present at all? And why return module; if previous part is not needed?

I see usage of:

var module = (function(module) {...})(module || {});

for cases when module wasn't already defined sometimes... Is that above case?

3
  • in case you want to extend a module? Commented Sep 3, 2014 at 20:13
  • yea, that piece is for those purpose, but some part seems redundant for me. Commented Sep 3, 2014 at 20:15
  • well...return module is needed , otherwise you wont expose the module... remember also , that javascript is function scoped. Commented Sep 3, 2014 at 20:18

2 Answers 2

3

This block of code is only used if module is already defined and you want to extend it (e.g., add a new method):

var module = (function (module) {
    module.prototype.something_else = function () {
    };
    return module;
})(module);

It accepts module as an input parameter and returns the extended module. The assignment of var module = then replaces the original module.

Answering your questions one by one:

Why var module = part present at all?

In the example above (taken from your first URL), var module = is not necessary. The simple example they provide is silly. You could accomplish the exact same thing with this:

module.prototype.something_else = function () {};

Using the above pattern to extend a class makes more sense with the examples provided in your second URL ( http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html ):

var MODULE = (function (my) {
    var old_moduleMethod = my.moduleMethod;

    my.moduleMethod = function () {
        // method override, has access to old through old_moduleMethod...
    };

    return my;
}(MODULE));

Immediate executing function creates a new scope where we can preserve the old method var old_moduleMethod = my.moduleMethod;

Next question:

And why return module; if previous part is not needed?

You need return module because otherwise the function wouldn't return anything and the var module = assignment would be set to an undefined value.

Next question:

for cases when module wasn't already defined sometimes... Is that above case?

Yes, that is the case. If module isn't defined then you would need to pass in an empty object. However, you typically would use a pattern to extend an object if it isn't defined in the first place. That would make no sense and would just complicate your code.

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

3 Comments

assignment of "var module =" then replaces the original module - I think that module point to same address in memory before assignment and after. Am I right? That's why I frustrated... Like var a = a;
You are correct, it does point to the same place in memory. Objects are passed by reference in JavaScript. That's why the particular example of extensibility from the first URL is silly. They might as well just set module.prototype.something_else = function () {};. I updated my answer a moment ago. The example provided in your second URL provides a clear use case where using the immediate executing function to extend the object has benefits. In that example, we are able to preserve the old method.
I updated my answer slightly. You are correct var module = is not necessary in the example provided in the first URL.
1

To understand, it helps to rename identifiers to get the equivalent

var module = (function (module_impl) {
    module_impl.prototype.something_else = function () {
    };
    return module_impl;
})(module || {});

module_impl is the object that is to hold the module's public API. So after populating it, the anonymous function returns it to the caller, which assigns it to the global module. This way the API is now available using module while any implementation details are hidden in the closure created by the anonymous function.

module || {} now is to allow augmentation: the first time it gets called, module is still undefined since the assignment to the return value of the anonymous function has not happened yet. Thus `module || {} evaluates to the second operand.

To further illustrate why the assignment is needed. The first time, the stub gets called, it is effectively the same as

var module = (function () {
    var module_impl = {};
     // ...
     return module_impl;
})());

5 Comments

my question is why have var module = part. As you do module_impl.prototype.xxx = it will be accessible through module even without var module = assignment...
No - module would not be defined, module_impl is local to the anonymous function and for the first time is the newly created object {}.
So without module || {} this var module = have no sense?
without module || {} you will get and undefined error.
Above blog articles don't use module || {} in examples and call code as "augmentation". I afraid that authors give striped example so I have a question...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.