Examples of variable and function declaration in JavaScript
As shown in the following example:
(You can use the Chrome browser, and then right-click F12/or review the elements. Call out the developer tool and enter the console)
(TIPS: Shift + Enter in the console can be used to wrap code in the middle)
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:
Explanation:
In JavaScript,
Function xxx () {// a bunch of code... //... var name2 = name; var name = name | "Xiao Zhang"; // a bunch of code}
The execution is equivalent to the following:
Function xxx () {var name2 = undefined; var name = undefined; // other var values will also be pushed to the very beginning. // a bunch of code... //... name2 = name; name = name | "Xiao Zhang"; // a bunch of code}
You can also refer to the Bootstrap Chinese network tutorial
How to declare variables and functions in JavaScript (hoist)
Date:
Author: Tie an (http://blog.csdn.net/renfufei)