Home > Web Front-end > JS Tutorial > In-depth analysis of the execution environment and scope chain of js functions_javascript skills

In-depth analysis of the execution environment and scope chain of js functions_javascript skills

WBOY
Release: 2016-05-16 18:42:48
Original
986 people have browsed it

Step 1. After definition: Each defined function has an intrinsic attribute [scope], which corresponds to a list of objects. The objects in the list can only be accessed internally.

For example: if we create a global function A, then A’s [Scope] internal property contains only one global object (Global Object), and if we create a new function B in A, then B’s [ Scope] attribute contains two objects, the Activation Object object of function A is in the front, and the global object (Global Object) is in the back.

In short, the order of the object list in the [Scope] attribute of a function is the Activation Object object of the upper layer function, then the upper layer, all the way to the outermost global object.


Step 2. Execution: When a function is executed, an executable object (Execution Object) will be automatically created and bound to a scope chain (Scope Chain). The scope chain will be established through the following two steps for identifier resolution.

First, copy the objects in the internal properties of the function object [Scope] to the scope chain in order.
Secondly, when the function is executed, a new Activation Object object will be created. This object contains the definition of this, parameters (arguments), and local variables (including named parameters). This Activation Object object will be placed The front of the scope chain.
So the final order in the Scope Chain is the Activation Object of this function, then the Activation Object of the upper layer function, then the Activation Object of the upper layer, until the Global Object.


When an identifier is encountered during the execution of js code, it will be searched in the scope chain of the execution context (Execution Context) based on the name of the identifier. Starting from the first object in the scope chain (the Activation Object of the function), if not found, search for the next object in the scope chain, and so on, until the definition of the identifier is found. If the last object in the scope, which is the Global Object, is not found after searching, an error will be thrown, prompting undefined.



Suggestions derived from this:

1. Try to use local variables. This is not just a matter of private attributes. Local variables are derived from the above process. It can be seen that it can reduce search time (note: in general, this does not include browser optimization behavior).

2. Avoid using the with statement. Because it will modify the scope chain of the execution context (Execution Context) and add an object (Variable Object) at the front. The same is true for the catch statement block in the try-catch statement.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template