The story of the JavaScript declaration period and the execution period of breadcrumbs

Source: Internet
Author: User

JS in everything objects, special objects (function types) in the Declaration and execution of the time will happen some wonderful things, as follows:

function A () {    var id = 1;        Function B () {        alert (++i);                  } return b;} var C = A ();

  

The above code, is declared in the global scope of a

Take function A from definition to execution as an example to illustrate these concepts.

    1. When defining function A, the JS interpreter sets the scope chain of function A to the "Environment" where A is located, and if A is a global function, the scope Only the Window object is in the chain.
    2. When the function A is executed , a is entered into the appropriate execution Environment (excution context).
    3. During the creation of the execution environment, a scope property, the scope of a, is first added for a, and the value is the scope chain in step 1th. That is, the scope chain of the a.scope=a.
    4. The execution environment then creates an active object (Call object). The active object is also an object with properties, but it does not have a prototype and is not directly accessible through JavaScript code. After the active object is created, add the active object to the top of the scope chain of a. At this point A's scope chain contains two objects: A's active object and the Window object.
    5. The next step is to add a arguments property on the active object, which holds the arguments passed when the function A is called.
    6. Finally, the reference of all function A's parameters and internal function B is added to the active object of a. In this step, the definition of function B is completed, so as in the 3rd step, the scope chain of function B is set to the environment defined by B, which is the scope of a. (so the B's scope is B's active object-->a--the Global Window object)

Go back to code var c = A (); At this point a () of the return value of the B function to C, and then C is very magical access to the inside of a private property I , which is the magic of closure .

Because the function has an attribute that creates a closed environment when it is invoked, in some cases this feature is also called "closure";

Closure Self-Understanding:

Closures should be an abstract concept, presumably referring to a closed environment that functions will create at execution time, and this environment is accessible only by his internal function.

In other words, an intrinsic function can access an external scope only through a closure

With regard to closure recycling, it should be the same as other objects that are recycled without being referenced. (That is, when an intrinsic function is externally referenced, the closure is not recycled if the external reference survives). 

One more thing to add to the implementation period is the Magic keyword new.

When a function is executed normally, this is the case:

function A () {var id = 1;  return ID;} Function B () {alert (hello);} var C = a ();   return 1;var d = B ();   Return underfined;//returns the return value if there is a return,//returns the default value underfined, return   ; also returns underfined 

  

When a function is executed in a special way, this is the case:

function A () {this.id = 1;    }  A.prototype.name = "Tianxia"; function B () {alert (hello); return;} var C = new A ();   return {id:1,name: "Tianxia"};var d = new B ();   Return underfined;//If you call the function with new in front of a function, a new object that hides the prototype member connected to the function is created, and this is bound to the new object. The This object is returned by default, if there is a return value or the return value is returned.

  

PS: Well, another record of some theory of things, the above is purely a side dish of personal understanding, if there is a problem, please a lot of advice ha.

The story of the JavaScript declaration period and the execution period of breadcrumbs

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.