0

I am trying to understand Javascript module patter, but I can't figure out the difference between parameters added to the anonymous function and parameters added at the end. So can someone please help me understand the difference between both? Thanks

Below is a module pattern example which implement both anon. function parameters (JQ, Yahoo) and module parameters shown at the end (JQuery, Yahoo).

var modularpattern = (function(JQ, Yahoo) {
    var sum = 0 ;

    return {
        add:function() {
            sum = sum + 1;
            return sum;
        },
        reset:function() {
            return sum = 0;    
        }  
    }   
}(JQuery, Yahoo));
3
  • Can you show us an example of the module pattern added to an anonymous function? Commented Aug 31, 2014 at 12:09
  • They are the same, but this way you can create aliases without polluting the outer scope. Commented Aug 31, 2014 at 12:13
  • There is no such thing as a "module parameter". They're simply arguments of a function call - understand IEFE Commented Aug 31, 2014 at 12:19

3 Answers 3

1

Logically your codes is equal to:

var func = function(JQ, Yahoo) { // Section 1
    var sum = 0 ;

    return {
        add:function() {
            sum = sum + 1;
            return sum;
        },
        reset:function() {
            return sum = 0;    
        }  
    }   
}

var modularpattern = func(JQuery, Yahoo);  // Section 2

So in section 1

  • JQ : A function local variable which is used as input argument
  • Yahoo : Exactly same as JQ

And in section 2 (In this section actually you invoke the function )

  • JQuery : An existing object in the global scope
  • Yahoo : Exactly same as JQuery

Why do developers implement like this:

All global objects is accessible inside function scopes but accessing to local variables is much faster than global variables. (This is called Localization)

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

2 Comments

So 'JQ' here is like a local variable limited to the module function scope but is not accessible from the Global scope, while JQuery is a global variable which is accessible from global scope? Also what will happen if I want to pass JQuery library to this module but entered JQ again instead of JQuery? At this point the compiler won't understand that I need to pass JQuery library right?
@MChan first question:you're perfectly correct. Second question: because JQ is not defined in the global scope so you'll get undefined in that function (in other words JQ in that function will be undefined) third question: yes Note JavaScript does not have compiler it has interpreter
1

I can't figure out the difference between parameters added to the anonymous function and parameters added at the end

  • The parameters added to the anonymous function are the names you're giving to these things inside your function

  • The parameters added at the end are the references to these objects

This means you can access a "safe(r)" reference, as it's less easily changed by other pieces of code

(function (bar) {
    // use bar here, not foo as it's your protected reference
    // but unless something else happens, bar === foo
}(foo);

Code using this pattern is good for several reasons

  • Keeps the namespace clean
  • If frameworks contain conflicts, gives you an "safe" environment to work in where you can use the default names

1 Comment

So bar is like a local var which is limited to the anon func scope, while foo is like a global variable which tell the compiler to pass foo library to this module correct?
0

I had work with many realizations of Module Pattern, but this one is the best way:

(function(global) {
    var somePrivate = 'foo';

    function somePrivateMethod() {}

    var myModule = function() {
        // Your logic here
    }

    global.myModule = myModule;
})(this);

1 Comment

What is the difference between global and this above? Shouldn't those refer to libraries being imported?

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.