------------------Closure of JavaScript functions

Source: Internet
Author: User
Tags closure

When it comes to closures, people often confuse anonymous functions with closures. A closure is a function that accesses a variable in the scope of another function by the right. The common way to create closures is to create another function inside one function, still in the previous

Createcomparisonfunction () function as an example

  

1             functioncreatecomparisonfunction (PropertyName) {2                     return function(object1,object2) {3                      var value1 = Object1[propertyname];   4 var value2 = Object2[propertyname]; 5                         if(value1<value2) {6                             return-1;7}Else if(value1>value2) {8                             return1;9}Else{Ten                             return0; One                         } A                     } -};

In the part of the identity, it accesses the external variable propertyname even if the function is returned, it can still be accessed to this variable (PropertyName), which is the scope that should be included in the scope chain for anonymous functions createcomparisonfunction ().

To understand several concepts that must master the scope

  Scope Chain:

When will it be generated? : A scope chain for a variable object is created when the contemporary code executes in an execution environment.

Function: An orderly access to functions and variables that are guaranteed access to the execution environment. The front end of the scope is always the variable object corresponding to the current execution environment;

  execution Environment: defines variables or functions that have access to other data to determine their respective behavior. Each environment has a variable object (variable object) that corresponds to it. Variables and functions defined in the environment are stored in this object. The execution environment is destroyed when the function is completed

  Variable object: saves variables and functions in the execution environment.

 Active object: If the execution environment is a function, its active object is used as a variable object. The active object contains only one variable at the beginning.

During function execution, the corresponding variable is found in the scope chain needed to read and write the value of the variable. Let's take a look at an example to understand each of these conceptual counterparts:

1    functionCompare (value1,value2) {2    3     if(value1<value2) {4         return-1;5}Else if(value1>value2) {6         return1;7}Else{8         return0;9     }Ten}
var result = compare (5,10);
  

   

The Compare function is defined first and then called in the global scope, and the current active object is a variable object when the Compare execution environment is a function. The active object by default is only arguments but it is not the same at this time (this,arguments,value1,value2);

The corresponding variable object in the global scope (compare, result); let's see!

<1> when defining the Compare function, creates a pre-contained global variable object scope chain, which is stored in the internal [Scope] property.

<2> when the Compare function is called, an execution environment is created for the function and then the scope chain of the execution environment is built from the object in the [Scope] property of the assignment function. Thereafter, another activity object is created and pushed to the front end of the chain of execution scope.

For the execution environment of the compare function, its scope chain contains two variable objects (local active and global variable objects). Obviously, a scope chain is essentially a pointer to a variable object list, which references but does not actually contain variable objects.

So whenever a variable is accessed, a variable with the corresponding name is searched from the scope chain. Generally speaking, when the function is finished. The local active object is destroyed and only the global scope is saved in memory. But closures are all different.

In closures, when an intrinsic function is defined inside a function, the intrinsic function adds the active object of the outer function to its own scope chain, so the anonymous function defined by Createcomparisonfunction (), which acts on the domain, contains the scope of the outer function.

1functioncreatecomparisonfunction (PropertyName) {2return function(object1,object2) {3varValue1 =Object1[propertyname];4varvalue2 =Object2[propertyname]; 5if(value1<value2) { 6return-1; 7}Else if(value1>value2) { 8return1; 9}Else{10return0;11                         }12                     }13};

When the following code executes, the outer function and the inner function contain the scope chain, as

1 var compare = Createcomparisonfunction ("name"); 2 var result = Compare ({name: "Nicholas"},{name: "Greg"});

During the execution of the code, the internal anonymous function returned by the variable compare is saved, and the scope chain contains three variable objects, (global variable object, external function-active object, anonymous function-active object);

So even after the return, the corresponding variable can still be accessed, because when the external function (createcomparisonfunction) execution completes, its active object still exists, and will not be destroyed, because it is referenced in the scope chain of the inner function;

Although the external function returns, its execution environment corresponding to the scope chain is destroyed, but its active object still exists. Until the anonymous function is destroyed, its createcomparisonfunction corresponding active object is destroyed.

1 var compare = Createcomparisonfunction ("name"); 2 var result = Compare ({name: "Nicholas"},{name: "Greg"}); 3 null;  The main is the manual assignment is empty, notification system for garbage disposal; As its scope chain is destroyed (in addition to the global scope chain).

Because the closure carries the scope of the function that contains it, it consumes more memory than the other functions. Excessive use of closures can result in excessive internal consumption.

------------------Closure of JavaScript functions

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.