The execution environment defines other data that variables or functions have permission to access, and determines their respective behavior. Each execution environment has a variable object associated with it.
The global execution environment is the most peripheral execution environment. Depending on the host environment in which the JavaScript implementation is located, the objects representing the execution environment are also different. In a web browser, the global execution environment is considered a window object. Therefore, all global variables and functions are created as properties and methods of window objects.
Variable object: All variables and functions defined in the environment are stored in this object.
Scope chain: When the code is executed in an environment, a scope chain of variable objects is created. The purpose of the scope chain is to ensure orderly access to all variables and functions that have permission to access to the execution environment. The front end of the scope chain is always a variable object in the environment where the code currently executed is located.
Active object: The active object only contains one variable at the beginning, that is, the arguments object. The next variable object in the scope chain comes from the inclusion (external) environment, and the next variable object comes from the next inclusion environment. This continues to the global execution environment; the variable objects of the global execution environment are always the last object in the scope chain.
Identifier parsing: Identifier parsing is a process of searching for identifiers one by one along the scope chain. The search process always starts from the front end of the scope chain and then goes back step by step until the identifier is found.
Sample code:
var color = "blue";function changeColor() { if (color === "blue") { color = "red"; } else { color = "blue"; }}changeColor();alert("Color is now " + color);The scope chain of the function changeColor() contains two objects: its own variable object (which defines arguments object) and a variable object of global variables. The variable color can be accessed inside the function because it can be found in this scope chain.
In addition, variables defined in local scopes can be used interchangeably with global variables in a local environment, example:
var color = "blue";function changeColor() { var anotherColor = "red"; function swapColors() { var tempColor = anotherColor; anotherColor = color; color = tempColor; // You can access color, anotherColor and tempColor here } // You can access color and anotherColor here, but you can not access tempColor swapColors();} // You can only access colorchangeColor();The above code involves 3 execution environments: the global environment, the handle environment of changeColor() and the local environment of swapColors().
There is a variable color and a function changeColor() in the global variable. The local variable of changeColor() contains a variable anotherColor and a function swapColors() function, which can access the color in the global variable. There is a variable tempColor in the local variable of swapColors(). In swapColors(), you can access the color in the global variable, or the otherColor variable, because those two environments are its parent execution environment. The scope chain of the above example is:
Among them, the internal environment can access all external environments through the scope chain, but the external environment cannot access any variables and functions in the internal environment . The connections between environment variables are linear and sequential. Each variable can only search for the scope chain from the superior to query the variable and function name, that is, first query the variable or function name in this function, and if it is not queryed to the previous scope chain until the top scope. However, no environment can search down the scope chain and enter another execution environment.
Function parameters are also treated as variables, so their access rules are the same as other variables in the execution environment.
1. Extend the scope chain
The scope chain is extended when the execution flow enters any of the following statements:
• catch block of try-catch statement
• with statement
These two statements will add a variable object to the front end of the scope.
For the with statement, the specified variable is added to the scope chain. For the catch statement, a new variable object is created, which contains the declaration of the wrong object being thrown.
For example:
function buildUrl() { var qs = "?debug=true"; with(location) { var url = href + qs; } return url;}The with statement receives a location object, so its variable object contains the properties and methods used by the location object, and this variable object is added to the front end of the scope chain. When referring to the variable href in the with statement (actually referring to location.href), it can be found in the current environment variable. When referring to the variable qs, the variable defined in buildUrl() is referenced, which is located in the function environment variable object. As for the with statement, a variable named url is defined, so url becomes part of the function execution environment and can be returned as the value of the function.
2. No block-level scope
In JavaScript, enclosed curly braces have no scope of their own. Look at the following code:
if(true) { var color = "blue";}alert(color); // "blue"In JavaScript, the variable declaration created by the if/for statement adds the variable to the current execution environment. For example:
for(var i = 0; i < 10; i++) { doSomething(i);}alert(i);// 10Garbage recycling
Similar to Java, JavaScript also has an automatic garbage recycling mechanism. The execution environment is responsible for managing the memory used during code execution. When writing programs, there is no need for related memory usage issues. The allocation of required memory and the recycling of useless memory are fully implemented. The principle of the garbage collection mechanism is: find out variables that are no longer used, and then free up the memory it occupies. To do this, the garbage collector will perform this operation periodically at a fixed time interval (or a predetermined collection time during code execution).
Before doing garbage collection, you must determine whether the resource is useless and mark variables that are no longer used in order to recycle their memory in the future. There are usually two implementations of strategies for identifying useless variables.
1 Mark clear
The most common way to garbage collection in JavaScript is marker cleaning. When a variable enters the environment, it is marked as "enter the environment"; when a variable leaves the environment, it is marked as "exit the environment". When the garbage collector runs, it marks all the variables used. It then removes the tags of variables in the environment and variables referenced by variables in the environment. After that, the tagged variables will be regarded as variables ready to be deleted. Finally, the garbage collector completes the memory clearing work, destroys the marked values and recycles the memory space they occupy.
2. Reference Count
Reference count refers to the number of times each value is referenced. When a variable is declared and a reference type value is assigned to the variable, the number of references to this value is 1. If the same value is assigned to another variable, the number of references to the value is increased by 1. Conversely, if the variable containing the reference to this value takes another variable, the number of references to this value is reduced by 1. When the number of references to this variable is 0, it means that there is no way to refer to this variable again, so its memory space can be reclaimed. The next time the garbage collector runs, it recycles the memory occupied by these values with zero references.
One problem that reference counting can cause recycled references. For example:
function problem() { var objA = new Object(); var objB = new Object(); objA.someOtherObj = objB; objB.someOtherObj = objA;}In the above example, objA and objB refer to each other through attributes. After the function execution is completed, objA and objB will continue to exist and their reference count will not be 0. This situation will cause the memory occupied by objA and objB to be recycled.
The above article briefly talks about JavaScript: execution environment, scope and garbage collection are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.