Home > Web Front-end > JS Tutorial > Examples of early declaration of variables or functions in JavaScript_javascript tips

Examples of early declaration of variables or functions in JavaScript_javascript tips

WBOY
Release: 2016-05-16 16:31:30
Original
1471 people have browsed it

As shown in the title, look at the example below.

(You can use the Chrome browser, then F12/or right-click, inspect the element. Call up the developer tools and enter the console input)
(Usage tips: Shift Enter can break the code midway during console input)

Copy code The code is as follows:

var name = "xiaoming";

(function(){
var name = name || "小张";
console.info(name);
})();// Xiao Zhang

(function(){
name = name || "小张";
console.info(name);
})(); // xiaoming

(function(){
var name2= name;
var name = name || "小张";
console.info(name, name2);
})(); // Xiao Zhang undefined

The screenshot during execution is as follows:

The explanation is as follows:

In JavaScript.

Copy code The code is as follows:

function xxx(){
// A bunch of code...
// ...
var name2 = name;
var name = name || "小张";
// A bunch of code
}

When executed, it will become this equivalent form:

Copy code The code is as follows:

function xxx(){
var name2 = undefined;
var name = undefined;
// Other vars will also be advanced to the beginning
// A bunch of code...
// ...
name2 = name;
name = name || "小张";
// A bunch of code
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template