Leave a comment if I’m wrong: In my memory, I thought the difference between the two style of function creation:
function myFunc(){};
and
myFunc = function(){};
is the first evaluate right away, and the later waits for the function call.
so I got this code in this article about global variable is attach to window object:
function setGloabalVariable(){
window.variable = '1';
}
after loading the page, why the variable still undefined?
The difference between these two types of declarations is that the first is a named function statement, the second is an anonymous function expression. Neither of them is automatically executed at their point of declaration (you may be thinking of an IIFE).
Both
my_named_functionandmy_anonymous_funcare unexecuted. Both may be executed by calling them:An IIFE (Immediately Executed Function Expression) works a little differently:
I recommend reading through kangax’s excellent article on function expressions and statements for more information on this subject.