K
K
Kirill Korotkov2016-01-14 20:15:31
JavaScript
Kirill Korotkov, 2016-01-14 20:15:31

Is there a difference in declaring an anonymous javascript wrapper function?

When studying the coffee-script language, the question arose: is there a difference in the declaration of an anonymous wrapper function on javacript, in the form of a self-calling function without explicitly specifying the call context:

(function(){
//какой-то код
})();

from declaring such a wrapper dictated by the coffee-script compiler?
(function(){
//какой-то код
}).call(this);

Is there any sense in indicating the context? Or does the javascript interpreter treat both declarations of such a function in the same way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gro, 2016-01-21
@gro

In the first one, in non-strict mode, this will be the global object. In strict mode - null (or undef, I don't remember).
In the second in the global scope, there will again be a global object. Inside a method, this is the method's this. Apparently, this is done to save the context inside the methods.
2016 is not the best year for learning cofeescript, learn es6 or typescript better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question