The definition and execution of JavaScript functions

Source: Internet
Author: User
Tags define function

To understand the definition and execution of JavaScript functions, you need to know these important concepts first, and now you can understand them later!

the execution environment of the function (Excution context), the active object (call object), scope (scope), scope chain (scope chain). Next, let's take this function as an example to analyze:Steps: 1. Set the scope chainWhen you define function A, the JS interpreter will Scope Chain (scope chain)Set to "Defines the environment where A is located", where a the first added scope is the Window object. ( If a is a global function, only the window object is in scope chain. ) Personal Insights:The scope chain is actually a contained active object, and the active object can be understood to be used to identify the scope. (like a shopping mall is divided into a, B, c three districts, it can be understood that in this mall's scope chain, there are A, B, c 3 active objects, 3 ranges!) ("Personal opinion may not be appropriate"). 2, the implementation of the EnvironmentWhen the function A is executed, a is entered into the appropriate execution environment (excution context). Personal Insights:Creating an execution environment is divided into Creating ScopesAnd Create an active objectTwo steps. 3. ScopeWhen creating an execution environment, a scope property, the scope of a, is first added to a, and the value is the scope chain in the first step. That is, the scope chain of A.scope = A. Personal Insights:Scopes and scope chains can be understood to be different names but with the same effect! 4. Create an active objectAfter the scope is created, the execution environment is immediately Create an active object (call object)。 The active object is also an object with properties, but it is not prototype and cannot be accessed directly through JavaScript code (see the following diagram or personal insights). 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. The next step is to add a arguments property on the active object, which holds the arguments passed when the function A is called. Finally, all the parameters of function A and the internal reference to function B are added to the active object of a. In this step, the definition of function B is completed, so as in step 3rd, the scope of function B is set to the environment defined by B, which is the scope of a. (The first joined scope in a scope chain like a is the Window object.) )! Personal Insights:(1) The active object is a noun added for understanding and does not exist, so it does not have a prototype and cannot be accessed with actual code. (similar to magnetic line, just to describe) End:This completes the entire function A from definition to execution. At this point a returns a reference to C for B, and because the scope chain of function B contains a reference to the active object of function A, which means that all the variables and functions defined in a are accessible in B. Function B is referenced by C, and function B relies on a, so function A is not recycled by GC (refer to the garbage collection mechanism at the bottom of JavaScript)! When function B is executed, it will be the same as the above steps. So at execution time, the scope chain of B consists of 3 objects: The active object of B, the active object of a, the Window object,: When the function B accesses a variable, the search order is: The prototype object of B's active object->b (exists)->a's active object Window object variable lookup mechanism:Finds the active object of its own, returns if it exists, and finds the prototype object if it does not exist and the function has a prototype prototype object. Find the active object, window object of a. Until it is found, returns undefined if the entire scope chain is not found. Summary: The above mentioned two important words: function definition and execution. The article mentions The scope of a function is determined when the function is defined, not when it is executed.(See steps 1 and 3). Use a piece of code to illustrate the problem:
 1  function   f (x) {  var  g = function  () {return   x;}  3  return   G;  4   5  var  h = f (1 6  alert (H ()); 
In this code, the variable h points to the anonymous function in F (returned by G, or it can be understood as not anonymous, because there is the name G (informal understanding)). (1) The scope of H is determined at the time of definition: The individual understands the scope chain of H: G's active object->f the active object->window object (because here H represents the return "G ()" function, since it is defined as the "g ()" function, so H.scope chain = G.scope chain) The scope chain of the online reference H: The active object->f object of H (2) The scope of H is determined when executing (alert (H ()): H's scope chain: H's Active object->window Gt;alert the active object->window object. Results: (1) should output 1 (2) should output undefined. The practice proves that the scope of the function is defined when defining the function!

Add: JavaScript's garbage collection mechanism

In JavaScript, if an object is no longer referenced, the object is recycled by the GC. If two objects are referenced by each other and are no longer referenced by the 3rd, then the two mutually referenced objects are also recycled. Because function A is referenced by B and B is referenced by a c outside of a, this is why function A is not recycled after execution.

function A () {

var i = 0;

var B = function () {alert (++i);}

return b;

}

var C = A ();

C ();

If the above code does not return B; When function A is finished, it will be recycled! Two objects refer to each other (a refers to b,b reference a) and are no longer referenced by the 3rd (no return data to C), then the two mutually referenced objects are also recycled.

Reference Document: http://www.jb51.net/article/24101.htm (JavaScript deep understanding JS closure)

The definition and execution of JavaScript functions

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.