As shown in title, see the following example
(You can use the Chrome browser and then f12/or right-click to review the elements.) Bring up the developer tool and enter the console console input.
(Tip: Shift+enter can break code in the middle of the console input)
var name = "Xiaoming"; (function () { var name = name | | "Xiao Zhang"; Console.info (name);}) ();//Xiao Zhang (function () { name = name | | "Xiao Zhang"; Console.info (name);}) (); Xiaoming (function () { var name2= name; var name = name | | "Xiao Zhang"; Console.info (name, name2);}) (); Xiao Zhang undefined
The execution is as follows:
The explanations are as follows:
In JavaScript,
function xxx () { //A heap of code ... // ... var name2 = name; var name = name | | "Xiao Zhang"; A bunch of code}
Execution will become this equivalent form:
function xxx () { var name2 = undefined; var name = undefined; Other Var will also be advanced to the beginning //heap of code ... // ... name2 = name; Name = Name | | "Xiao Zhang"; A bunch of code}
You can also refer to the Bootstrap Chinese web tutorial
advanced in JavaScript for variable and function declarations (hoist)
Date: 2014-11-07
Author: anchor (HTTP://BLOG.CSDN.NET/RENFUFEI)
Early examples of variables and function declarations in JavaScript