Closed Package
function Createcomparisonfunction (PropertyName) {returnfunction (Object1, object2) {varValue1 =Object1[propertyname]; varvalue2 =Object2[propertyname]; if(Value1 <value2) { return-1; } Else if(Value1 >value2) { return 1; } Else { return 0; } };}//Create a functionvarCompare = Createcomparisonfunction ("name");//calling Functionsvarresult = Compare ({name:"Jim"}, {name:"Jack" });//dereference an anonymous function to free up memoryCompare =NULL;View Code
After the anonymous function is returned from Createcomparisonfunction (), its scope chain is initialized to the active object and the global variable object that contains the Createcomparisonfunction () function.
In this way, the anonymous function can access all the variables defined in Createcomparisonfunction (), and the active object is not destroyed when the createcomparisonfunction () function finishes executing. This activity object is still referenced because of the scope chain of the anonymous object.
The active object of the Createcomparisonfunction () function will not be destroyed until the anonymous function is destroyed.
Closures and variables
In the following code snippet, in fragment 1, the active object of the Createfunctions () function is saved in the scope of each function, so they all apply the same variable i. When the createfunctions () function finishes executing, I is 5, so the value of I within each anonymous function is 5
In fragment 2, outside the returned anonymous function, an anonymous function is created, and the result of executing the anonymous function is immediately assigned to the array. The function arguments are passed by value, and the current value of the variable i is assigned to NUM, inside the anonymous function, creating a closure that accesses the NUM variable.
//Code Snippet 1function Createfunctions () {varresult =NewArray (); for(vari =0; I <5; i++) {Result[i]=function () {alert (i); } } returnresult;}varFuncs =createfunctions ();//Array.foreach ()Funcs.foreach (function (item) {item ();});//5,5,5,5,5//Code Snippet 2function Createfunctions () {varresult =NewArray (); for(vari =0; I <5; i++) {Result[i]=function (num) {returnfunction () {alert (num); }} (i); } returnresult;}varFuncs =createfunctions ();//Array.foreach ()Funcs.foreach (function (item) {item ();});//0,1,2,3,4View CodeThis in a closed package
var name = " the window " var object = {name: " my Object " , Getnamefunc:function () { return F Unction () { return this .name; }}};alert ( object . Getnamefunc () ()); //
View Code
Object.getnamefunc () returns an anonymous function that calls the function in the global environment, which refers to the global object
To solve this problem, you can save the this object in the external scope of the anonymous function in a variable that the closure can access
varName ="The Window";var Object={name:"My Object", Getnamefunc:function () {varthat = This; returnfunction () {returnThat.name; }}};alert (Object. Getnamefunc () ());//"My Object"View Code
"JavaScript advanced Programming" closure of Reading notes