Search code examples
javascriptnamespacesjavascript-namespacesrevealing-module-pattern

Calling a namespaced function by its fully qualified name from inside same namespace


Is there any reason you would ever want to call a function defined inside a namespace created using the revealing module pattern using its fully qualified name? For example, A vs. B below?

Example A.

var namespace = (function defineNamespace() {

    function sayNoThankYou() { }

    function callMeMaybe() {
        sayNoThankYou();
    }

    return {
        callMeMaybe: callMeMaybe,
        sayNoThankYou: sayNoThankYou
    };

}());

Example B.

var namespace = (function defineNamespace() {

    function sayNoThankYou() { }

    function callMeMaybe() {
        namespace.sayNoThankYou();
    }

    return {
        callMeMaybe: callMeMaybe,
        sayNoThankYou: sayNoThankYou
    };

}());

Solution

  • There is only one difference between namespace.sayNoThankYou(); and sayNoThankYou(); in your example: the value of this inside the sayNoThankYou function. If you don't use this inside the function, they are identical.