The more I learn JS, the more I feel how important the basic theoretical knowledge of JS is. Therefore, I have spent more time to learn recently, and recorded the feelings gained from learning, in order to deepen my understanding, but also for my future review. Please point out the wrong places. I am very glad to communicate with you.

  • 1. Execution environment
  • 2. Scope
  • 3. Variable objects

1. Execution environment

The execution environment is the environment in which a function or variable is running when JS is running. The execution environment defines other data that variables or functions have access to, determines their respective behavior, and determines their lifetime. Execution environment can be divided into function execution environment and global execution environment.

Function execution environment: Each time a function is called, the execution environment of that function is created in memory, and the function environment is pushed into an environment stack. When the function completes execution, the stack pops up its environment and destroys it, returning control to the previous execution environment. Global execution environment: is the most peripheral execution environment. In web browsers, the window object is considered the global execution environment. All global variables and functions are properties and methods of the window object. The global execution environment is destroyed when the application exits (web page/browser is closed).

2. Scope

Scope: Represents the area that a variable or function can be accessed, controlling the visibility and life cycle of variables and parameters (or the set of rules for finding variables by name). It is generally divided into local scope (function scope) and global scope. Function scope guarantees that arguments and variables within a function are not visible outside the function, and that variables defined anywhere in the function are visible anywhere in the function (variable declaration promotion). Lexical scope: Defines the scope at the lexical stage (breaking up a string of characters into meaningful code blocks called lexical units). In other words, the lexical scope is determined by where you write the variable and block scope when you write the code.

Advantages:

1. Reduces naming conflicts and provides automatic memory management. Inner functions have access to arguments and variables (except this and arguments) that define their outer functions.

Scoped chain: When code executes in an environment, a scoped chain of variable objects is created to ensure orderly access to all variables and functions that are entitled to access in the execution environment. The essence of a scope chain is a list of Pointers to variable objects that reference but don’t actually contain them. The top of the scope is Window in the browser environment and Global in the Node environment. The very front end of the scope is always the variable object of the environment in which the code is currently executing.

Assignment to a variable performs two actions: first, the compiler declares a variable in the current scope (if it has been declared before, the compiler ignores the declaration and continues compiling), then at runtime the engine looks for the variable in the scope and assigns it a value if it can find it.

3. Variable objects

Variable object: Each execution environment has a variable object associated with it, which contains all variables and functions defined in the execution environment. If the execution environment is a function, its active object is treated as a variable object. Active objects start with a single variable ———— Anguments (not available in the global environment). The next variable object in the scope chain comes from the containing environment, and so continues to the variable object in the global execution environment. Creating variable objects is the process of elevating variables and functions. Active object: variable object in the current function execution environment. It just needs to be activated when the function is called and initializes arguments. Activation objects and variable objects are the same thing, but the variable object is specified or implemented by the engine and cannot be accessed in the JavaScript environment. Only when an execution context is entered can the variable object in that execution context be activated. Only the active variable object, that is, the various properties on the active object, can be accessed. An active object is created when the function context is entered, and it is initialized via the function arguments property. Arguments property values are arguments objects.

When entering the execution context, the variable object includes these objects before the code is executed:

1. Arguments to a function (if function context)

A. An attribute of a variable object consisting of a name and corresponding value is created. B. If there is no argument, the property value is set to undefined

2. Function declaration

(a) A property consisting of the name and corresponding value (function-object) of a variable object is created. (b) If the variable object already has a property of the same name, the property is completely replaced

3. Variable declarations

A. An attribute of a variable object consisting of a name and its corresponding value (undefined) is created; B. If the name of the variable is the same as the formal parameter or function already declared, the variable declaration does not interfere with the existing class of attributes

Here’s an example:

function compare(value1,value2){ if(value1<value){ return -1; }else if(value1>value2){ return 1; }else{ return 0; }} var result = compare (5, 10)Copy the code

The following figure shows the execution environment of the above code and the relationship between the scope chain and the variable object (screenshot from the book, I am lazy).

1. JavaScript deep variable objects