One of the JavaScript (rolled up) reading notes you don't know----scope

Source: Internet
Author: User

You don't know. JavaScript (roll up) This book seems to me to be a good book, the book in a relatively concise language to describe the JS of those "pits", here to write some blog record notes for digestion and absorption.

1 Compilation Principle

In this book, the beginning of the proposed: JavaScript is a compiler language, I started to think this is a domestic translation of the pot, translation is not accurate, and later I also dedicated to GitHub to see, the author is actually JS described as a compiler language (compiled language). However, I think the author would like to express that JS also has a Java-specific compilation process . Rather than the traditional thought of simply "interpreting execution".

A compiled language is defined as a compiled language that first compiles the source code into a machine language, and then the machine runs the machine language, as in C/s + +.

Java is an interpreted language because it compiles the source code into bytecode that can be executed by the JVM, which is clearly incompatible with the definition of a compiled language. JavaScript also has a similar process to Java, but the process is short. This completes the program's compilation within a few microseconds of the code execution, and then executes it immediately.

The compilation of JavaScript is divided into three steps:

A. Participle/lexical analysis

B. Analytic/syntactic analysis

c. Code Generation

JavaScript uses various methods (JIT, deferred compilation, recompilation, etc.) to ensure the best performance during the parsing and code generation phases of the compilation phase.

2 Understanding Scopes

The interpretation and execution of JavaScript in the book is mainly involved in several roles:

Engine : Responsible for the first part of the compilation (Word breaker/lexical analysis) and code that executes after compilation

Compiler : responsible for the second part of compilation (parsing) and the third part of compilation (code generation)

Scope: Responsible for collecting all declared identifiers, i.e. variables, and maintaining a set of rules for managing variables
for var a = 2, you can think of his execution as follows

A. The first step of the engine to perform the compilation of participle/lexical analysis

B. Engine invoke compiler, compiler for compiling syntax parsing and code generation

A) The compiler encounters Var A to find out if a variable exists in the current scope collection

b) If present, the variable declaration is ignored

c) If it does not exist, declare a new variable in the scope named a

d) generate the code required for the engine to run

C. The code generated by the engine run

A) go to the current scope to find out if a variable is present, and if the variable is not found, it will continue to look up (its upper scope)

b) If present, use this variable and assign a value to the variable

c) If not present, throws an exception or creates a variable (* see below for details LHS and RHS)

Note: There are two ways in which the engine goes to scope to look for variables, LHS queries, and RHS queries.

A) LHS can be thought of as the source address (the variable is actually a contiguous address in the computer's memory, and we name the memory address by naming the variable), and the source address is used to assign a value to the variable.

b) RHS represents the value of the variable, which is typically used to assign values to other variables

Directly said LHS and RHS must be too abstract, it is not easy to remember, LHS and RHS is literally the difference between L and R, respectively, representing left and right

For the line of code a= B = 2, the code first assigns a value of B to 2. And then what? here's the point.

1 First query Gets the value of B

2 assigning the obtained value of a query to a

And this, exactly corresponding to the RHS query and LHS query, first on the right side of the B RHS (query to get the value of B), and then LHS query to find the source address of a variable (for a assignment).

function execution, such as Foo (2), is actually a RHS to Foo, the resulting value is the function type of the object, and then go to execute him, there is a time (multipleparameters are multiple) of the formal parameter lhs.

3 Scope Nesting

When a block or function is nested within another block or function, the scope is nested, so if we define a function in a block, the function can be called to a variable that is in the outer part of the function during execution.

The whole chain of functions is like a pyramid in which the author likens a function-scoped chain to a building. At the top level of the scope is a global scope, each of which may contain several sub-scopes, extending so so that the pyramid is more appropriate for the function scope chain.

lhs and RHS are searched in the current scope first, and if no results are found, they will always look up until the global scope.

4 Exceptions

If the engine is not found in the top-most global scope, what behavior does it have? In this case, LHS and RHS will behave differently.

A. For LHS: If this variable is not found at the top level, if it is in non-strict mode, a global variable is created in the global scope, and if strict mode prevents the global variable from being created automatically or implicitly, the Referenceerror exception is thrown.

B. For RHS: If the variable is not found at the top level, a Referenceerror exception is thrown. An TypeError exception is thrown if an unreasonable operation is made on a variable that is queried, such as calling a non-function object.

One of the JavaScript (rolled up) reading notes you don't know----scope

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.