Share JAVASCRIPT function scopes and advance declarations

Source: Internet
Author: User

Some languages such as C and java have block-level scopes, that is, each piece of code in curly brackets has its own scopes, and variables are invisible outside the code segments that declare them, however, javascript does not have block-level scope. Javascript uses function scopes, that is, variables are defined in the declared function bodies and any function nested in the function bodies, that is, all variables declared in 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 javascript Functions are advanced to the top of the function. Let's look at an example.
Copy codeThe Code is as follows:
Var test1 = "globalVariable ";
Function test (){
Console. log (test1 );
Var test1 = "localVariable ";
Console. log (test1 );
}

The result of the above function execution is: output "undefined" first, and then output "localVariable ".
Many people mistakenly think that the result is: output "globalVariable first, then output localVariable ". Otherwise, due to the function scope, local variables are always defined throughout the function body, that is, local variables in the function body cover global variables with the same name, however, only when the program executes the var statement will the local variable be assigned a value. Therefore, the above process is equivalent to bringing the variable declaration in the function to the top of the function body, and the variable initialization remains at the original position. This is equivalent to the following function:
Copy codeThe Code is as follows:
Var test1 = "globalVariable ";
Function test (){
Var test1; // advance the variable declaration in the function to the top of the Function
Console. log (test1 );
Test1 = "localVariable"; // value assignment
Console. log (test1 );
}

However, if the var variable is not declared in the function, the situation is different.
Copy codeThe Code is as follows:
Var test1 = "globalVariable ";
Function test (){
Console. log (test1 );
Test1 = "localVariable ";
Console. log (test1 );
}

The result of this function is: output "globalVariable" first, and then "localVariable ".
Because the test1 variable in the function does not use var declaration, it is a global variable by default. Of course, there is no issue of variable declaration in advance. The first line will output "globalVariable", while the third line changes the value of the global variable test1 and outputs "localVariable ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.