Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        }
    };
}();
like image 652
john_science Avatar asked Jan 23 '13 21:01

john_science


1 Answers

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).

like image 69
Bergi Avatar answered Oct 16 '22 13:10

Bergi