javascript--Scope

Source: Internet
Author: User

    • Inside the function: a local variable declared with Var, and Var is a global variable.
    • The scope depends on the definition of the variable, not when it is executed.

Example 1:

The results are as follows:

Note: at (1) Although the local variable A is not defined, and the outside global variable is already defined, but inside the function f, the local variable itself already has local space, so (1) access to A is a local variable.

Example 2:

Note: b in F2 is undefined, whereas B in F1 is a local variable, so the variables between the function F1 and F2 are not mutually accessible. When the global variable b=2 is defined, the execution of B in F1 and the global variable B is two different variables, and B in F2 actually operates a global variable.

You can declare a variable in the function F1 as a global variable. As follows:

javascript--Scope

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.