Dom Notes (eight): JavaScript execution Environment and garbage collection

Source: Internet
Author: User
Tags color identifier getcolor

First, the implementation of the environment

Another concept that can be divorced from the point of reference to a JavaScript object or this is the execution environment, which is the context. The execution environment is a very important concept in JavaScript because it defines the other data that variables or functions have access to, and determines their behavior. Each execution environment has a variable object associated with it, and all variables and functions defined in that environment are stored in the object.

An execution environment that is the outermost of the global environment. In a Web browser, the global environment is considered a Window object (which is discussed in subsequent notes), so all global variables and functions are created as properties or methods of the Window object. The global execution environment will not be destroyed until the application exits, such as by closing the Web page or browser.

Corresponding to it is the local environment. Each function forms a local environment, and when all the code is executed, the environment is destroyed, and all of the variables and function definitions are destroyed.

1. Scope Chain

When code executes in an environment, it creates a scope chain for the object. The front end of a scope chain is always the environment variable object where the code currently executes, and the last object of the scope chain is always a variable object of the global environment. It is important to note that for a function, its active object contains the arguments object, which is not in the global environment.

var   color=< Span style= "COLOR: #006080" > "blue" ; function  ChangeColor () {var  anothercolor=<     Span style= "COLOR: #006080" > "red" ; function  Swapcolor () {var  tempcolor =          Anothercolor;          Anothercolor = color;     color = Tempcolor; } swapcolor ();} ChangeColor ()  

The above code involves a total of 3 execution environments: The global environment, the local environment of the ChangeColor (), and the local environment of the Swapcolor (). The scope chain is as follows:

The outermost is the Global Environment Object window (the blue part), which contains the variable color and function ChangeColor. The local Environment of ChangeColor () contains variable anothercolor and function Swapcolor. The local Environment of Swapcolor () contains a variable tempcolor that can only be accessed in this environment. 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.

2. Extend the scope chain

There is no concept of block scope in JavaScript, so variables declared in the If, for, and other block statements are added to the current execution environment.

 for (var i=0; i<10; i++) {    dosth (i);} alert (i);    The value of I is ten

I in the above code is added to the global environment. In JavaScript, variables declared with Var are automatically added to the closest environment. Within the function, the closest environment is the local environment of the function, and in the With statement, the closest environment is the function environment. If there is no Var declaration, it is added to the global environment.

However, some statements can extend the chain of action by adding a temporary variable to the front end of the scope chain, and the temporary variable is deleted after the code executes.

2.1 The Catch block of the Try-catch statement.

2.2 With statement.

For a catch statement, a new variable object is created that contains the declaration of the Error object being thrown, and for the WITH statement, the specified object is added to the scope chain.

function BuildUrl () {    var"? debug=true";    With (location)    {        var url = href+qs;    }    return URL;}

A URL variable is defined within with, and the URL is automatically added to the function execution environment, so this variable can be used as the return value of the function.

3. Query identifier

When you reference an identifier by reading or writing in an environment, you must search to determine what the identifier represents. The search process starts at the front end of the scope chain and queries upwards. If found in a local environment, the search is stopped, and conversely, a variable object that continues to search up the scope chain and goes back to the global environment. If the identifier is found in the global environment, it is not declared.

var color="Blue"; function GetColor () {    return color;} Alert (GetColor ());  Blue

Find the color identifier in GetColor () first, or search up if not found.

Second, garbage collection

JavaScript has an automatic garbage collection mechanism, and the execution environment is responsible for managing the memory used during code execution, which is simple: identify variables that are no longer in use, and then release their memory. Therefore, the garbage collector performs this operation periodically at a fixed time interval or during the scheduled collection time in code execution.

1. Mark Clear

The most common method of garbage collection in JavaScript is tag cleanup. The garbage collector adds tags to all variables stored in memory at run time, then removes variables from the environment and tags of variables referenced by variables in the environment. Variables that are added later will be treated as variables that are ready to be deleted because they cannot be accessed. Finally, the garbage collector completes the memory cleanup work. The JavaScript engines of IE, FF, Chrome, Safari, and opera are all implemented this way.

2. Reference counting

The reference count is the number of times each value is referenced by the tracking record. When declaring a variable and assigning a reference type value to the variable, the reference number of the value is added by 1, instead, if the variable that contains the value refers to another value, the number of references to that value is reduced by 1. When the number of references becomes 0 o'clock, the garbage collector clears the space occupied by the variable. However, this approach causes circular references.

function  var  oa = new  Object;    var  ob = new  Object;    Oa.another = OB; Ob.anotherobject = OA;}  

  OA and OB reference each other, when the function is finished, the two objects will never be 0 references, if you call this function repeatedly, it will cause a lot of memory can not be recycled. So this approach is not often used. It should be noted that when it comes to COM objects in IE, there may be circular references, because some of IE's objects are not native JavaScript objects, such as BOM and Dom are in C + + COM (Component object model, Component object models) objects in the form of a COM object that uses reference counting.

var element = document.getElementById ("xxx"); var New Object (); myobject.element=element;element.some=myobject;

However, you can avoid circular references by manually disconnecting the connections between native JavaScript objects and DOM elements.

myobject.element=null; element.some=null;

Set the variable to NULL to break the connection between the variable and the previously referenced value. The next time the garbage collector runs, the values are deleted and the memory they occupy is reclaimed.

Original starting: http://www.ido321.com/1345.html

Next: "Translate" the drop-down menu under the response

Dom Notes (eight): JavaScript execution Environment and garbage collection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.