JS Scope 1

Source: Internet
Author: User

-# #1. What is a scope # #

Any programming language can store variables and modify the values of variables, but how to introduce variables is an issue where variables are stored where to find variables.

To better let us find where these variables are stored, and how to get the program to find variables, we create a series of rules that we call scopes;

# # #1.1 Compiling principle # # #

Each code goes through three steps before it executes.

1. Word Segmentation/Lexical analysis
2. Analytic/syntactic analysis
3. Code generation


* * Word segmentation/Lexical analysis * *

This process breaks down the character string code into meaningful blocks of code that we call lexical units, such as:

var a = 2;

This code will be decomposed into Var, a, =, 2,; Whether a space can be treated as a lexical unit depends on the space's meaning in the code;

* * Parsing/syntax unit * *

The process is to change the set of lexical units into a tree of grammatical rules that consists of nesting elements (we call this tree abstract syntax tree, or AST), for example

Var
/
X
/
=
/
2

* * Code Generation * *

The process is to transform the tree we parse into executable code, throw away the different languages and different platforms, simply, the var a = 2 of the abstract syntax tree into a machine-readable instruction, let the machine create a variable, and assign a value of 2;

* * Key points * *

Unlike other languages, JS code does not take a lot of time to optimize, because JS compilation is not executed in advance, most of the time the code is compiled in the code before the execution of a very small period of time (a few microseconds or even less time), so any JS code will be compiled before execution, And will usually be executed immediately after compilation.

JS Scope 1

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.