Each of the JavaScript functions is an instance of a function object that has an internal property that is accessible only to the JavaScript engine [[Scope]]. This [[Scope]] stores a collection of scopes called "Scope Chains", which store "variable object" VO or "active object" AO (ao ratio vo more this and arguments attributes) in the collection.
When a function is created, all mutable objects in the scope chain of its parent scope are added to its [[scope]] (if the parent scope is global, only one global object is added to the scope chain of the current function).
When the function is executed, the execution environment of the function is pushed into an environment stack:
At this point the variable initialization phase of the function is entered, and this stage determines the function's interior: This value, the function's arguments, the function's declaration, the Declaration of the variable, and the arguments. The active object AO, which makes up the current function according to these values, then saves the AO object to the first of the current function's scope chain.
Note: The AO is populated in the following order:
- function parameters (if there is a parameter, it will be assigned, if the parameter is not passed, the initialization value is undefined) priority second
- function declaration (overrides if a naming conflict occurs) highest priority
- Variable declaration (the initialization variable value is undefined, if a naming conflict occurs, it is ignored) priority third
Then, at the execution stage of the function, all the variables and function declarations used in this stage of the current function are looked up from the [scope] scope chain of the current function, and based on the position of the object in the scope chain, the AO object of the current function is first found, and if no further upper object is searched, the global object is If none, the error will be (the variable is undefined).
The first time to write a microblog, there are wrong or missing place also please correct me.
The process of JavaScript function execution