Another discussion on the scope of JS
Topics encountered in the interview: Topic one: var word = "Hello world"; (function () {alert (word); var word = "Hello test"; })();
Effect http://keleyi.com/keleyi/phtml/js/1.htm
Topic Two: var word = "Hello world"; (function () {alert (world); function World () {console.log ("Hello Test"); } })()
Effect http://keleyi.com/keleyi/phtml/js/1b.htm
What happens with these two pieces of code? Console a bit, you know.
The precedence of local variables is greater than global variables.
JS compilation mechanism JS is divided into the compilation period and run time, the process of compiling for VAR declared variables and *function* allocation of memory, run-time sequence execution.
var declares the variable, only the variable declaration is advanced. The initialization of the variable is still in its original position.
With function declaration statements, function names and function bodies are advanced. (In a function declaration statement, the function name is a variable name, pointing to the body of the function)
This article is reproduced from Collayi http://keleyi.com/a/bjad/cydygkca.htm
Another discussion on the scope of JS