In lot's of JavaScript libraries I see the following code:
(function(context){
})(this);
Why do these libraries use this? And what does this code do?
In lot's of JavaScript libraries I see the following code:
(function(context){
})(this);
Why do these libraries use this? And what does this code do?
That is an IEFE function which receives this
as a parameter.
What it does is avoid scoping/closure issues by referring to context
instead of this
which varies when used in different places.
By referring to context
you can be sure that it will remain this
passed as argument and nothing else. But if you use this
directly in the code, then closure and scoping issues will arise.