JavaScript templates: function call context

I have a ton of JavaScript from the start with function calls written as follows:

THING.someFunction.call(THING); 

It seems to me that this should always be equivalent:

 THING.someFunction(); 

Are these two calls always equivalent? What about older javascript versions?

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

To be clear, THING is defined like this:

 var THING = function () { // private vars return{ // code someFunction : function () { // code } }; }(); 
+6
source share
2 answers

Yes, they are equivalent. And I don't know any version of JavaScript that they weren't in (however, call seems to be added in 1.3).

+1
source

They are equally technically. But they also behave a little differently in asynchronous programming. call () is used to call a function by passing the area as a parameter. This provides a convenient way to call certain functions in callbacks and deferred execution (setTimeout, setInterval). If you used any of the JS libraries, you would notice $ .proxy or _.bind, these are aliases that implement the call (scope);

See this MDN document for more details.

0
source

All Articles