As shown in the title, see the example below.
(You can use Chrome browser, then F12/or right-click to review the elements. Call out the developer tools and enter the console console to enter)
(Usage tips: Shift+Enter can be used to break the line when entering the console)
The code copy is as follows:
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 screenshot during execution is as follows:
The explanation is as follows:
In JavaScript.
The code copy is as follows:
function xxx(){
// A bunch of code...
// ...
var name2 = name;
var name = name || "Xiao Zhang";
// A bunch of codes
}
This is the equivalent form when executed:
The code copy is as follows:
function xxx(){
var name2 = undefined;
var name = undefined;
// Other vars will also be advanced to the starting point
// A bunch of code...
// ...
name2 = name;
name = name || "Xiao Zhang";
// A bunch of codes
}