JavaScript parsing order and variable scope

Source: Internet
Author: User

JavaScript parsing order and variable scope

The scope of a variable

1. Global variables: The resulting variable can be accessed anywhere in the code, with a global scope.
A. Variables defined outside the outermost function.
B. There are no variables that define a direct assignment, and have global properties.

2. Local variables: can only be accessed in a fixed code fragment (in a function fragment).
A. A variable that is defined inside a function is a local variable.
B. The parameter is also a local variable.

Second, JavaScript pre-parse order
1, <Script></script> block in turn to parse;
2, parsing the environment of code operation;
3, the identifier (keyword) (var function) to parse, resolved to the corresponding environment;
4, if there are <Script></script> block and then follow the above steps to parse.

Third, the noun explanation
(1) Environment:

1. Hosting environment
2. Execution Environment

The execution environment determines the access rights of variables and functions.

A. Global Environment
B. Function environment
C.eval ();

(2) Scope

The scope of a piece of code.

(3) Scope chain

Variables and functions that have access in an execution environment can be accessed in an orderly and organic manner.





JavaScript parsing order and variable 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.