2

I've been following this tutoral, and when referencing Module as an argument in ModuleTwo, everything works fine until I comment out Module.

My understanding is that the double pipes || and empty object {} will create an empty object in place of Module if it's undefined, but instead I'm getting an error in the console.

var Module = (function () {

  var privateMethod = function () {
    // private
  };

  var someMethod = function () {
    // public
  };

  var anotherMethod = function () {
    // public
  };

  return {
    someMethod: someMethod,
    anotherMethod: anotherMethod
  };

})();

var ModuleTwo = (function (Module) {

    Module.extension = function () {
        // another method!
    };

    return Module;

})(Module || {});
2
  • 1
    What is the error you're getting in the console? Commented Aug 7, 2015 at 19:31
  • When you say you "comment out Module", do you mean the variable declaration (i.e. ` var Module = ...) or are you commenting out the (Module || {} )` line?... Commented Aug 7, 2015 at 19:34

2 Answers 2

2

Basically, there’s an error in the tutorial. One way to make things work was suggested by rplantiko, but it might be easier to just write window.Module || {} instead of Module || {}.

How things work here:

  • Accessing a non-existent property of any object yields undefined. However, referencing a variable that hasn’t been declared yields ReferenceError (so your understanding was a little bit off there).
  • Browser puts all global variables as properties onto global window object. Module in the tutorial is a global variable, because it’s declared outside all functions, so you can access it via window.Module, which will not cause ReferenceError if undefined (per previous point).

It might be a good practice to explicitly assign to window any global variable you define (e.g., do window.Module = (function () { … if you intend to make Module global), but that’s arguable and out of scope of this discussion.

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

1 Comment

sure, glad if it helped.
2

When you apply the function defining your 2nd module to (Module || {}), the symbol Module cannot be resolved if the Module hasn't been declared earlier, which always gives a JavaScript error. If you want the 2nd Module to be defined even in the absence of the first Module, try the following:

var ModuleTwo = (function(Module) {
   ...
   })(typeof Module == 'object' ? Module : {} ); 

3 Comments

Thank you! What is the purpose of adding the || {} then?
As you expected, you can use the idiom a = b || c for the intention "assign b to a, but only if b doesn't evaluate to false (or null or "" or 0 or...). If it does evaluate to false, the logical or is forced to evaluate c, and then c will be the result of the evaluation of the right-hand side. If b was not false, b will be the result of the rhs. But in your case, you can't use this idiom, because for an evaluation of an expression, all symbols must be known (except as argument of the typeof operator, hence my proposal above).
Thanks a ton! I really appreciate all the help.

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.