Experience Summary:
1, the declaration of the variable is advanced to the top of the scope, the assignment is retained in situ;
2, the function declaration is in advance;
3. When the function is assigned to a variable as a value, only the variable is "advanced" and the function is not "advanced".
As a best practice:
Variable declarations must be placed at the top of the scope/function (JavaScript has only function scope).
Example:
//The variable declaration is advanced to the top of the scope and the assignment remains in place. varA = ' out ';(functioninside () {alert (' Before: ' + a);//undefined varA = ' in '; Alert (' After: ' + a);//' in '} ()) Alert (' Out: ' + a);//' Out '//equivalentvarA = ' out ';(functioninside () {varA//declaration in advanceAlert (' Before: ' + a);//A is not initialized, so it is undefinedA = ' in '; Alert (' After: ' + a);//A is assigned a value of ' in '} ()) Alert (' Out: ' + a);
//the function declaration is in advance//function declaration format: Functions fun () {...}Fun ();//' Hello,world. 'functionFun () {alert (' Hello,world. ')); }fun (); //' Hello,world. '//when the function is assigned to a variable as a value, only the variable is advanced and the function is not advancedFun ();//TypeError error, stating that fun is not a functionvarFun =function() {alert (' Hello,world. ')); };fun (); //' Hello,world. 'equivalent to:varFun;fun (); //Fun hasn't been initialized yet, so it's not a functionFun =function() {alert (' Hello,world. ')); };fun (); //' Hello,world. '
The specific details of this knowledge point is probably so much, seriously think about experience.
Reference:
The topic of knowledge
Blog 1
Some official documentation
Variables and function declaration advance questions