JavaScript lexical, static, dynamic scope primary understanding

Source: Internet
Author: User
Tags variable scope

Before you begin

Because I am also a JavaScript beginner, record learning pass, afraid later will forget.

For JavaScript beginners, the hardest part is not the Code section, but the understanding of the terminology in many books, and most of the time you want to understand what is explained in a JavaScript book, it involves a lot of terminology, In the absence of the terminology will be the content of the understanding of the deviation (resulting in difficult to remember the content of the study), because I read books often encounter this problem, so today will write down this basic content.

One, need to pay attention to the point

1) Execution Environment (scope) function execution environment variable scope

2) function scope and declaration in advance

3) Free variable

4) lexical scope and static scope

5) Dynamic Scope

Second, explain

1) Execution Environment (scope) Variable scope

When it comes to the word execution environment, it's not strange for a classmate who has seen advanced programming in JavaScript, so let's look at the execution environment first:

1. Implementation environment: The "Environment" is the most important concept in JavaScript. The execution environment defines the other data that a variable or function has access to, determines their behavior, and each execution environment has an associated variable object (variable object), and all variables and functions defined in the environment are stored in the object. The code we write does not have access to this object, but the parser uses it in the background when it processes the data.

    2. The global execution environment is the outermost execution environment in which the global execution environment is considered a Window object, so all global variables and functions are created as properties and methods of the Window object. After all the code in an execution environment is executed, the environment is destroyed, and all the variables and function definitions stored therein are destroyed.

 3. Function execution Environment: each function has its own execution environment, and when the execution flow enters a function, the environment of the function is pushed into an environment stack. After the function executes, the stack pops up its environment and returns control to the previous execution environment.

4. Scope Chain: Its purpose is to ensure an orderly access to all variables and functions that the execution environment has access to, the front end of the scope chain, which is always the variable object of the environment in which the code is currently executing. When code executes in an execution environment, a scope chain of variable objects is created (scope chain).

2) Declaration in advance

1. Declaration in advance: the function scope of JavaScript is that all variables declared within a function are always visible in the function body, which means that the variable is available before the declaration, which is also the reason. This is done when the JavaScript is "precompiled", before the code runs.

In the case where the local variable takes precedence over the global variable//function nesting, the most inner function variable takes precedence over the function variable var on it scope = ' global scope '; function Checkscope () {    //body ...      var scope = ' local scope ';    function nested () {        var scope = ' nested scope ';        return scope;    }    return nested ();} var scope = Checkscope (); Console.log (' varable scope: ', scope)//' nested scope '

3) Free variables: The variable x used in a scope is not declared in a scope (that is, declared in other scopes), and for a scope, X is a free variable.

AR x = 10;function fn () {var b = 20;console.log (x + b);//x is a free variable. }

4) Lexical scopes and static scopes: Lexical scopes are equivalent to static scopes, and static scope rules are dependent on static relationships between blocks in the source program when they look for a variable declaration.

5) Dynamic scope rules depend on the sequence of function calls that are executed by the program.

An important difference between a static scope and a dynamic scope is:
Static scope rules Find a variable declaration depends on the static relationship between blocks in the source program;
Dynamic scope rules, however, depend on the sequence of function calls at program execution time.
The point is that the static scope looks for a declaration of the same name identifier in the outer scope closest to the current scope,
The dynamic scope is to find the most recent activity record

About the scope of several more than applauded articles:



JavaScript lexical, static, dynamic scope primary understanding

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.