"You don't know JavaScript" reading notes (II.) lexical scopes

Source: Internet
Author: User

JavaScript uses the lexical scope of the working model.

Defined

Lexical: Most standard language compiler's first working stage is called lexical (word), this process will check the characters in the source code, if it is stateful parsing process, but also give meaning to the word.

Lexical scope: Defines the scope in the lexical phase .

The lexical scope is determined by WHO: where you write the variables and block scopes When you write the code. Therefore, in most cases, the lexical analyzer retains its effect when it processes the code.

Cases

function foo (a) {    var2;    function Bar (c) {        Console.log (A, B, c);    }     3  2 );

Output: 2,4,12.

Parsing: There are three nested scopes in this example, such as:

① contains the entire global scope, where there is only one identifier: foo

② contains a scope created by Foo, with three identifiers: A,b,bar

The ③ contains the scope created by bar and has an identifier: C

Scopes are determined by where the corresponding scope block code is written, and they are included hierarchically.

Note: There are no scopes that can occur simultaneously or partially in two external scopes.

A scope lookup stops when the first matching identifier is found.

"You don't know JavaScript" reading notes (II.) lexical scopes

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.