JS variables and their scopes, JS variable scopes

Source: Internet
Author: User

JS variables and their scopes, JS variable scopes

1. variables and their scopes: variables are classified into "global variables" and "local variables". The "global variables" are declared outside the function and can be used by all functions, the "local variable" is declared within the function body and can only be used in the function that defines the variable.

(Note: When declaring a variable, there is no var keyword, but the variables directly assigned values are global variables)

<Script type = "text/javascript"> function main () {n = 10; // here, n is a global variable and can be directly used externally} main (); alert (n); </script>

2. In the function body, the priority of local variables is higher than that of global variables.

<Script type = "text/javascript"> // an example I think is very representative on the Internet. variables with the same name are declared both inside and outside the function body, for example, var n = 1; function test () {alert (n); // here, a is not a global variable, the reason is that the fourth row of the function declares a local variable with the same duplicate name // a. If the Declaration of the Fourth Row a is commented out, the expression a here shows 1, which is a global variable. So I have to // come to the conclusion that global variable a is overwritten by local variable. // It indicates that before the JS function test () is executed, variable a in the function directs to a local variable, but the output in this // row is not assigned a value during execution, so undefined is displayed. N = 2; alert (n); var n; // returns the local variable a alert (n) ;}test (); alert (n); </script>

According to my understanding, the final output answer of the above example should be: 1 2 2 1; but the correct answer is: undefined 2 2 2 1; this is because local variables overwrite global variables when variables with the same name are declared both inside and outside the function body.

3. How can I read the local variables inside the function body from the outside?

Generally, only the function body can directly obtain external global variables, but it cannot obtain local variables inside the function body. However, by defining a function inside the function body to return a local variable, you can call the function externally.

<Script type = "text/javascript"> function f1 () {var n = 10; function f2 () {// define f2 () inside f1 (), access the local variable alert (n);} return f2; // return the f1 () local variable n} var result = f1 (); // call the f1 () function externally to obtain the result () of the local variable n; // 10, that is, the value of n </script>

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.