Personal Understanding:
The concept of an execution Environment : Each function creates its own execution environment when invoked. When the execution stream enters a function, the environment of the function is pushed into an environment stack, and after the function is executed, the stack pops up its execution environment and transfers control to the previous execution environment. The execution environment defines the other data that the variables and functions have access to and defines their respective behavior;
Variable Objects : When a function is called, each execution environment has a variable object associated with it, and all variables and objects defined in the environment are saved in this object.
The Variable object (variable object) is the scope of data that is related to the execution context. It is a special object associated with the context that stores variables (variables) and function declarations (functions declarations) that are defined in the context. It is an abstract concept, in different contexts, which represents the use of different object. For example, in global context, a variable object is also the global object itself [Global object]. (This is where we can point to global variables through the properties of the global object).
scope chain : When a function is created, a scoped chain that contains a global variable object is created, and the scope chain is stored in the internal "scope" attribute, so that when you create any function, you create a scope chain that contains the global variable object;
The first time the function is called, creates an execution environment and the corresponding scope chain, then builds the scope chain of the execution environment by copying the object in the scope property of the function, and then an active object (here as a variable object) is created and pushed into the front-end of the execution environment scope chain, Finally, the function activity object is initialized with This,arguments, and other named parameters (if it is a function, its active object is used as a variable object);
Nature : 1, the front end of the scope chain is always the variable object in the environment where the code is currently executing
2, the variable object of the global execution environment is the last variable object of the scope chain;
3, the role of the scope chain is to ensure the sequential access to all variables and functions in the execution environment;
4, the internal environment variable can access all the external environment through the scope chain, but the external environment cannot access any variables and functions of the internal environment;
5, the nature of the scope chain is a list of pointers to variable objects, and he only references but does not actually contain variable objects.
Choice of Niang:
execution Context Stack : Note that a function can produce an infinite context, because the invocation of one of the functions (even recursion) creates a new context.
The execution context of a series of activities logically forms a stack. The bottom of the stack is always the global context , and the top of the stack is the current (active) execution context. When switching between different execution contexts (exiting and entering the new execution context), the stack is modified (either through a stack or a fallback).
When a JavaScript code file is loaded by the browser, the default first entry is a global execution context . When a function is called in the global context, the program flow enters the called function, at which point the engine creates a new execution context for the function and presses it onto the top of the execution context stack. The browser always executes the current context at the top of the stack, and once executed, the context is ejected from the top of the stack, and then the context under which the code executes. The context in the stack is then executed sequentially and the stack pops up until it returns to the global context.
Execution Context : Also known as the execution link, an execution context can be abstracted as object. Each execution context has a series of attributes (which we call the context state) that they use to track the execution progress of the associated code. There are three main properties: Variable object (Variable object), this pointer (this value), scope chain (scope chain).
Active Object
When the function is activated by the caller, this particular active object (activation object) is created. It contains generic parameters (formal parameters) and special parameters (arguments) objects (parameter mapping tables with indexed properties). The active object is used as a variable object in the function context.
That is, the variable object of the function remains unchanged, but the addition of the storage variable to the function declaration also contains the arguments of the special object.
scope chain Optimization : It can be seen from the structure of the scope chain that the deeper the identifier is in the scope chain of the runtime context, the slower the read and write speed will be. Because global variables always exist at the very end of the run-time context-scoped chain, finding global variables is the slowest when identifiers are parsed. So, when writing code, you should use as little global variables as possible, and use local variables whenever you can. A good rule of thumb is that if a cross scoped object is referenced more than once, it is stored in a local variable for reuse.