Let’s first look at the following 3 pieces of code
var firstName = "Mark ";
(function DisplayFirstName() {
console.log(firstName);
})();//Must output Mark
var lastName = "Aut";
(function DisplayLastName () {
var lastName = "Bru";
console.log(lastName);
})();//Bru must be output, and the local scope has a higher priority than the global scope
//What about the following code?
var lastName = "Aut";
(function DisplayLastName() {
console.log(lastName);
var lastName = "Bru";
console.log(lastName);
})();//Who can guess what the result is?
The result of this output is:
LOG: undefined
LOG: Bru
This is beyond my expectation. I originally thought it should be "Aut" and "Bru"
My original understanding was: when the program first outputs lastName, The program did not find the locally declared lastName variable, so it used the global lastName definition. The value of the local variable
was used the second time (because in my concept, JavaScript is an interpreted language, sentence by sentence... ....execution)
Seeing this result, it seems that the execution of javascript is not always sequential..
So far, based on my guess, javascript execution should first perform syntax analysis, Then I analyzed and completed the variable table (local and global)
and then started to execute the script line by line in sequence
I also ask all javascript experts to clarify my doubts.
Statement: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