Elevation (4): Execution environment, scope, context execution process, garbage collection, Try...catch ...

Source: Internet
Author: User

Gao Cheng 4.2, 4.3

First, the implementation of the environment

1, the global execution environment is the outermost execution environment.

2, each function has its own execution environment, when the function is executed, the function environment will be pushed into a current environment stack, execution is completed, the stack will pop up its environment, the controller back to the previous execution environment.

Second, scope

1. Each execution environment corresponds to one of its own scopes.

2, each execution environment, can access the parent of the previous level, parent 、... Scope, called a "scope chain"

3. When executing code, the identifier is searched up and down the scope chain at the first level. If you can't find it, you'll get an error.

* Marker: The variable or function to use when executing

Third, the context of the implementation process

The compiler performs three steps before the code executes after entering the execution environment:

1. Find and promote the declaration of "All parameters of function" (if there is a row parameter)

2. Find and promote the declaration of "All functions inside the execution Environment"

3. Find and promote the declaration of "All variables inside the execution environment"

Last Example:

functionShow (A, b) {//Three steps:        /*///1: Find and promote the declaration of "All line parameters of function" a = function () {console.log (' AA ');};     b = undefined; */        /*///2: Find and promote the declaration of "All functions within the execution Environment"//the point at which a is replaced by a function that is pointed to in 1 a = functions () {console.log (' a222 ');} */    /*///3: Find and promote declaration of "All variables inside the execution Environment"//because the variable priority does not have a high function, it is not possible to replace the A variable with the above as the function's point B = undefined; */alert (a);//function A () {Console.log (' a222 ');}    functionA () {Console.log (' a222 ')); }    varA = 123; varb = 222;  alert (a); //123alert (b);//222}show (function() {console.log (' AA ');});

Note: In the above example, the 3rd step is due to the precedence of the variable declaration elevation, there is no function declaration that the elevation is higher, so even if the same name variable A is found, it cannot be replaced with the previously defined a function

Refer to: http://www.cnblogs.com/TomXu/archive/2012/01/16/2309728.html

Iv. Collection of Garbage

JavaScript has an automatic garbage collection mechanism

1, the principle: identify those who do not need to continue to use the variable, and then release its occupied memory.

To do this, the garbage collector periodically performs this operation at a fixed time interval (or a predetermined collection time in code execution).

2, the general engine adopts two policies: Mark Clear, reference count

Note: Although, JS has an automatic garbage collection mechanism, but for performance optimization, you can do a bit:

A: After the object or function or event is used, resetting to NULL means that the connection between the variable and its previously referenced value is cut off, and the next time the garbage collector runs, the values are removed and the memory they occupy is reclaimed.

V. Try...catch (ex) ...

Try: Catch Exception statement

Catch: Exception-following processing statement

functionShow () {varmsg = ' parameter is at least one '; Try {        if(Arguments.length = = 0) {            Throw NewError (msg); //the statement following the exception cannot be executedConsole.log (' Error ... '); }        //does not affect execution outside of ifConsole.log (' Go on ... ');//output go on ...}Catch(ex) {Console.log (ex); Throw NewError (msg); }}

Attention:

1, if the exception thrown inside will only affect, if the inside behind the execution. Without affecting statement execution outside of IF.

2. Try...catch ... Commonly used in plugins.

Elevation (4): Execution environment, scope, context execution process, garbage collection, Try...catch ...

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.