Search code examples
javascriptfunctioncall

JavaScript Patterns: Context of Function Call


I have a ton of JavaScript from the dawn of time with function calls written like this:

THING.someFunction.call(THING);

It seems to me that should always be equivalent to:

THING.someFunction();

Are these two calls always equivalent? What about old versions of JavaScript?

It seems to me the purpose of the second THING in that first line of code is to set the context (this) inside someFunction. But the context inside that function should already be THING by default, right?

Just to be clear, THING is defined something like this:

var THING = function () {
    // private vars

    return{
        // code
        someFunction : function () {
            // code
        }
    };
}();

Solution

  • Yes, they are equivalent. And I don't know any version of JavaScript in which they were not (however, call seems to have been added in 1.3).