1

This is the module pattern:

var module1 = (function(){

    // private variable
    var private_var;

    // private method
    var private_method = function(){
        // ...
    }

    // public interface
    return {
      // A public variable
      publicVar: "foo",

      // A public function utilizing privates
      publicMethod: function( bar ) {
          // ..
      }
})()

I've found a similar pattern where the return value is not an object but a function:

var module2 = (function(){

    // private variable
    var private_var;

    // private method
    var private_method = function(){
        // ...
    }

    // this is supposed to be a shortcut for me.doThings
    var me = function (options) {
      me.doThings(options); 
    }

    me.doThings = function(options) {
       // do something
    } 

    // returns a function instead of an object
    return me;      

 })()

used like this: module2(options).

me is in fact a shortcut for the me.doThings function.
I'm confused if this could be considered a Module pattern at all. And what would be the main differences and use cases between module1 and module2

1
  • The only difference between module1 and 2 is that module2 is only return a single function. Module pattern returns an object, but in js a function is an object too. Commented Nov 26, 2014 at 20:40

2 Answers 2

2

I would consider both methods you illustrate to be the module pattern because you are returning an object in both cases (remember functions are objects and can also have properties).

Your example in module2 is useful when you want to return a primary constructor function that may also have properties. This particular construct is used very often in node modules which use the same general module concept (slightly different syntax in creating the module, but the same principle). It is not necessary to use this construct, because the function could always just be made a property of the main object, but it can sometimes lead to shorter, more compact code if all the module is doing is define a function.

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

2 Comments

Would you please clarify the last sentence "It is not necessary..." it's not easy to follow, what object are you talking about when you say "the main object"? Any example of different use cases?
@Leonardo - Your module1 can return as many functions as it wants as properties of the object it is returning so the top level function you are returning in module2 can always be a property of the object in module1. The difference between the two is only in convenience and syntax for access of one function. The same capabilities are present either way.
0

In JavaScript every function is actually a Function object and like any other javascript Object it can store properties and methods.

The method me() is only a shortcut for me. doThings() because me() is implicitly calling it. In your example, doThings() is a method of the function me().

Here is a modified example that hopefully will clarify what I mean:

var module2 = (function(){

    // private variable
    var private_var;

    // private method
    var private_method = function(){
        // ...
    }

    // this is supposed to be a shortcut for me.doThings
    var me = function (options) {
      // Anything can happen here. For example you can treat me as a method to define a private var within the module:
      private_var = options;
    }

    me.doThings = function(options) {
       // do something
       console.log('do something', private_var);
    } 

    me.doMoreThings = function(options) {
       // do even more of something
       console.log('do even more of something', private_var);
    } 

    // returns a function instead of an object
    return me;      

 })();

module2('Hello World');
module2.doThings(); // 'do something','Hello world'
module2.doMoreThings(); // 'do even more of something','Hello world'

module2('Hello Universe');
module2.doThings(); // 'do something','Hello Universe'
module2.doMoreThings(); // 'do even more of something','Hello Universe'

1 Comment

@jfriend00 answer definitely described it better than I could. :D

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.