Memory space management data type structure execution context
Variable object active object Scope scope chain closure
Unlike other languages, JS refers to data types, such as array arrays, whose values are not fixed in size. The value of the reference data type is the object that is saved in the heap memory
The execution context can be understood as the environment in which the function executes, and each function executes, creating such an execution environment for the corresponding function.
Global context out of stack after browser window closes
The lifecycle of the execution context, the creation phase: Creating a Variable object, establishing a scope chain, and determining this point to the execution phase: variable assignment, function reference, other code invocation activation, starts creating the corresponding execution context, in the process of execution context generation, the variable object, the scope chain, And the value of this will be determined separately. We already know that when a function is invoked (activated), a new execution context is created. The lifecycle of an execution context can be divided into two phases.
Creation phase in this phase, the execution context creates the variable object, establishes the scope chain, and determines the point of this.
Once the code execution phase is created, the execution of the code begins, at which point the variable assignment, function reference, and other code are executed.
The creation of variable objects, followed by several procedures. Create a arguments object. Checks the parameters in the current context to establish the properties and property values under the object. Checks the function declaration of the current context, which is the function declared using the functions keyword. In a variable object, establish a property with the function name, which is a reference to the memory address where the function resides. If the property of the function name already exists, the property will be overwritten by the new reference. Check the variable declaration in the current context, each found a variable declaration, in the variable object in the variable name to establish a property, the property value is undefined. If the property of the variable name already exists, to prevent a function with the same name from being modified to undefined, it is skipped and the original property value is not modified. The scope chain, which consists of a series of variable objects in the current environment and the upper environment, guarantees the orderly access of the current execution environment to the variables and functions that meet the access rights. The scope chain is made up of a series of variable objects, in which we can query the identifiers in the variable object so that we can access the variables in the previous scope.
Many people misunderstand that the current scope and upper scope are inclusive, but they are not. The one-direction channel with the most front-end as the starting point and the end point is more apt to describe it. All of the ends are global variable objects.
After entering the execution phase, the variable object is converted to the active object, the attributes inside can be accessed, and then the operation of the execution phase begins.
If you are asked the difference between the variable object and the active object in the interview, then you can answer it freely, they are all the same object, but in the different lifecycle of the execution context. However, only variable objects that are in the execution context at the top of the function call stack become active objects.
Original address: http://www.jianshu.com/p/cd3fee40ef59