This article mainly introduces the Execution Environment (SCOPE) and scope chain in javascript, and summarizes them at the end of the article, if you are interested, you can see that many beginners cannot understand the execution environment and scope chain in javascript. Here, I will share it with you based on my own understanding.
Generally, we divide the execution environment into a global execution environment and a local execution environment. In this environment, we can also call it a function execution environment. So what makes the execution environment? Generally speaking, the execution environment is the environment where the code is executed. Let's take a look at the following code for further analysis.
《script》
var name="zhuzhenwei";function changeName(){ if (name=="zhuzhenwei"){ name="heting"; }else{ name="zhuzhenwei"; }}changeName();console.log(name); //heting
《script》
As in the preceding Code, when executing the first statement, the statement is in a global execution environment. Note that each execution environment has a variable object associated with it, for the global execution environment, the objects associated with it are window objects. Next, the following statement declares a function (Note: Only the function is declared here, and internal code is not executed before it is called ). This function is also in a global execution environment. Finally, we call the changeName () function. Once this function is called, the system immediately jumps to the execution environment of the changeName () function (that is, the function execution environment). Once it enters the execution environment, then, create the corresponding variables in the function (for example, if var a = 12 appears in the function, the related variable object is considered as an activity object (the activity object starts to contain only one variable, that is, the arguments object), and the statements are executed from top to bottom. At the same time, when the code is executed in the function environment, a scope chain of the variable object will be created. This scope chain contains the variable object of changeName () and the global variable object.
The scope chain is actually the range that we can access from the front end to the end, that is, to ensure orderly access to all variables and functions that the execution environment has the right to access, the front-end is the variable object where the code is currently executed. Here, it is the variable object of the changeName () function, and the end is the global variable object. For example, when we execute a function, you need to find an identifier. This is to look for it through the end of the scope chain. If the end Of the scope chain cannot be found, it will continue to look up until the window object. Although I did not mention the scope chain in the global environment, the scope chain actually exists in the global environment, but there is only one global variable object. Obviously: accessing local variables is faster than accessing global variables, because you do not need to search for the scope chain. Obviously, the scope chain dynamically changes with the execution environment of the Code.
After the changeName () function is executed, that is, after the code in the function execution environment is completed, the local variables and local objects in the environment will be destroyed immediately (if the variables are not declared using var, indicates that it is a global variable and will not be destroyed after code is executed in a local environment). Then, the execution environment is switched from the function execution environment to the global execution environment and the console is continued. log (name); statement. However, if we close the Web page or browser, the global environment will also be destroyed.
Summary:
The execution environment also becomes the scope, and the execution environment determines the lifecycle of the variable.
The execution environment can be divided into a global execution environment and a local execution environment. Every time you enter an execution environment, a scope chain is created for searching variables and functions, so we think that the scope chain is dynamic.
The local environment of a function not only has the right to access the variables in the function scope, but also has the right to access its contained environment (parent environment) and even the global environment; in the global environment, only variables and functions defined in the global environment can be accessed. (variables that are not declared using var in a local environment are also global variables ), data in a local environment cannot be accessed (not all data is said because global variables can be accessed ). Note: parameters in a function are partial variables of the function.
These are all the content of the Execution Environment (SCOPE) and scope chain in javascript. I hope the content in this article will help you in your study or work. If you have any questions, please leave a message, for more information, see the PHP Chinese website (www.php1.cn )!