I used to use the anonymous self-execute function in js:
((function(){
//do something
})();
However I found this somewhere:
((function(){
//do something
}).call(this);
What is the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the first example,
thiswill be the global object (windowin browser-land), unless you’re in ES5 mode, in which it’ll beundefined.In the second example,
thisdepends on whatthiswas in the calling context- eitherglobal(undefinedin ES5), or a object instance.[Fiddle]
You’ll also find the
.call()call is microscopically slower due to the extra work the engine has to do to set thethiscontext up.