Let's look at the following 3 codes
var firstName = "Mark";
(function DisplayFirstName() {
(firstName);
})();//The inevitable output Mark
var lastName = "Aut";
(function DisplayLastName() {
var lastName = "Bru";
(lastName);
})();//The necessary output Bru, the priority of the local scope is higher than the global scope
//What about the following code?
var lastName = "Aut";
(function DisplayLastName() {
(lastName);
var lastName = "Bru";
(lastName);
})();//Who can guess what the result is?
The result of this output is:
LOG: undefined
LOG: Bru
This was beyond my expectations. I thought it should be "Aut" and "Bru"
My original understanding is: When the program first outputs lastName, the program does not find the locally declared lastName variable, so the global lastName definition is used, and the value of the local variable is used the second time.
(Because in my concept, javascript is an interpreted language, sentence by sentence...execution)
Seeing this result, it seems that JavaScript execution is not all in sequence.
So far, as far as I guess, javascript execution should first do syntax analysis, and then analyze the variable table (local and global)
Then start executing one line of scripts in sequence
Please also ask all javascript experts to solve the problem
Copy the codeThe code is as follows:
var firstName = "Mark";
(function DisplayFirstName() {
(firstName);
})();//The inevitable output Mark
var lastName = "Aut";
(function DisplayLastName() {
var lastName = "Bru";
(lastName);
})();//The necessary output Bru, the priority of the local scope is higher than the global scope
//What about the following code?
var lastName = "Aut";
(function DisplayLastName() {
(lastName);
var lastName = "Bru";
(lastName);
})();//Who can guess what the result is?
The result of this output is:
LOG: undefined
LOG: Bru
This was beyond my expectations. I thought it should be "Aut" and "Bru"
My original understanding is: When the program first outputs lastName, the program does not find the locally declared lastName variable, so the global lastName definition is used, and the value of the local variable is used the second time.
(Because in my concept, javascript is an interpreted language, sentence by sentence...execution)
Seeing this result, it seems that JavaScript execution is not all in sequence.
So far, as far as I guess, javascript execution should first do syntax analysis, and then analyze the variable table (local and global)
Then start executing one line of scripts in sequence
Please also ask all javascript experts to solve the problem