Search code examples
javascriptjqueryprototypejs

Nested method call attached to an instance of DOMWindow instead of self


someSingleton = (function() {    
    var someFunction = function() {
        console.log(this);
        someOtherFunc();
    };

    var someOtherFunc = function() {
        console.log(this);
    };

    return {
        method: someFunction
    }
})();

someSingleton.method();

If run you this you'll notice the first method will return the object as expected and the second nested method call, someOtherFunction, will return the DOMWindow object.

Other than passing the instance (this) as a parameter to the second method, how do I make it so that the second method call is referencing the containing object and not DOMWindow.


Solution

  • someOtherFunc.call(this);

    It depends only on how the function is called, not where or how it's defined.