What is the difference between the following 2 examples of code:
(function(){
var myFunc = (function(){
//do something
})();
window.myFunc = myFunc;
})();
and
var myFunc = (function(){
//do something
})();
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.
The two ways are really similar, but there is a small difference on how the
myFuncglobal variable is created.In the second way, using the
varstatement, will create themyFuncvariable as a non-deleteable property of the global object, the var statement explicitly sets the internal{DontDelete}attribute , e.g.:While the first one can be deleted:
If you try the above in Firebug, both can be deleted, thats because Firebug uses code evaluation (
eval) in the console.You can check the above example here.
Recommended article: