1. Garbage collection
JS garbage collection mechanism is different from java,c and other languages need manual recovery, JS garbage collection is automatically started, the approximate process: JS interpreter will determine whether an object is accessible, that is, if there is a variable point to it, no, then start the recycling mechanism, release the memory occupied by the object, eg:
1 var " Hello World " ; 2 var s2 = s.touppercase (); // Create a new string: "HELLO World" 3 s2 = s; // "Hello World" does not have a variable pointing, it takes up memory to be freed
In this way, the JS code will not worry about, even if the definition of a lot of garbage objects, JS interpreter will be automatically recycled, to ensure efficient use of memory
2. Variables and attributes
Change of concept: The variable in JS is the attribute
① Global object When the JS interpreter does not execute any code, the global object is created: Global object, which can get a reference to the global object using this or window, where n multiple properties point to n variables or methods, such as This.parseint (); Point to method, this. Infinity points to an infinity variable, and when we define a global variable, the change amount defaults to a property of the global object.
② Local object function internally defined variables, that is, local variables, the actual local object Call object's properties, as above, the object's properties will also point to n variables (local) and even the internal nested function reference
3. Scope Chain--scope Chain
First, a concept, the execution context, that is, the execution environment of the JS execution script, the JS code that does not belong to any function is executed in a global execution context, and the internal code of global object object;function is executed under the local execution context. There is a local object call object in this environment
JS, the execution context is related to the scope chain, scope chain (scope chain) is a series of objects, such as JS to access the value of a variable, then to access its scope chain of the object's properties, to see if there is a property consistent with the variable, the process is the following code:
1 var 1 ; 2 function Test1 () {3 var 2 ; 4 function Test3 () {5 var3; 6 }7 }
The execution context of the global variable x is associated with the scope chain object only for global object, accessing X, then accessing the global objects if there is an X attribute, having, returning its value, no, returning undefined;
The execution context of the local variable y is associated with a Scopechain object that has two objects: the local object (Call object) where the function is located, the global object on the previous layer, and the Access Y, which first looks at the local object call object whether there is a property, Returns a value of 2, and if no more global objects are accessed, the global object does not have that property again, returning undefined;
For a local variable z, in the same way, the scope chain of its execution context is composed of three objects, the function contains the call object, the upper call object, the outer global object, the process of traversing the object properties is the same as in the procedure above, no longer repeat.
Today I learned these things, more biased theory, but feel that there is a lot of new things to learn, I hope to help you
Read "JavaScript---The Definitive Guide" summary notes
Js_ garbage collection, variables and attributes, scope chains