Home  >  Article  >  Web Front-end  >  JAVASCRIPT Function Scope and Advance Declaration Share_Basic Knowledge

JAVASCRIPT Function Scope and Advance Declaration Share_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 17:24:461107browse

Some languages ​​such as C and Java have block-level scope, that is, each piece of code within curly braces has its own scope, and variables are not visible outside the code segment where they are declared, but JavaScript does not have block-level scope. . JavaScript uses function scope, that is, variables are defined in the function body where they are declared and any function body in which this function body is nested. That is, all variables declared within the function are always visible in the function body. This means that variables can be used before they are declared. This feature is called "declaration advance", that is, all variables declared in a JavaScript function are advanced to the top of the function. Let’s look at an example.

Copy code The code is as follows:

 var test1 = "globalVariable";
 function test (){
 console.log(test1);
 var test1 = "localVariable";
  console.log(test1);
 }

The above function is executed The result is: "undefined" is output first, and then "localVariable" is output.
Many people mistakenly think that the result is: output "globalVariable first, then localVariable". In fact, it is not the case. Due to the characteristics of function scope, local variables are always defined in the entire function body. That is, local variables in the function body cover the global variables with the same name, but only when the program executes the var statement, the local variables are defined. will be truly assigned. Therefore, the above process is equivalent to advancing the variable declaration within the function to the top of the function body, while leaving the variable initialization at the original position. It is equivalent to the following function
Copy code The code is as follows:

 var test1 = "globalVariable" ;
Function test(){
var test1; // Advance the variable declaration within the function to the top of the function
console.log(test1);
test1 = "localVariable"; // Assignment
  console.log(test1);
 }

However, if the variable is not declared with var in the function, the situation is different.
Copy code The code is as follows:

 var test1 = "globalVariable";
 function test (){
  console.log(test1);
test1 = "localVariable";
console.log(test1);
}

The result of executing this function Yes: Output "globalVariable" first, then "localVariable".
Since the test1 variable in the function body is not declared with var, it defaults to a global variable. Of course, there is no problem of early declaration of variables. The first line will output "globalVariable", and the third line changes the value of the test1 global variable and outputs "localVariable".
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