Tag: The Comm window script performs the params parameter binding pre
Closed Package
closure: refers to a function that has access to a variable in another function scope.
A common way to create closures is to create another function inside one function:
function Createcomparisonfunction (propertyname) {return function (obj1, Obj2) { //accessed the variables in the external function var value1 = Obj1[propertyname]; var value2 = Obj2[propertyname]; if (value1 < value2) {return -1;} else if (value1 > value2) { return 1;} else {return 0;};}
The above obj1, OBJ2 accesses the variable propertyname of the external function. This variable is accessible because the scope of the intrinsic function contains the scope of Createcomparisonfunction ().
When a function is called, an execution environment and the corresponding scope chain are created. Then, use the values of arguments and other named parameters to initialize the active object of the function.
During function execution, to read and write the value of a variable, you need to find the variable in the scope chain.
function compare(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; }}var result = compare(5, 10);
The Compare () function is called first, and then it is called at the global scope, and when compare () is called, a live object containing arguments, value1, and value2 is created. The variable object for the global execution environment is second in the scope chain of the Compare () execution environment.
The scope chain at which the compare () function executes:
A function defined inside another function adds the active object containing the function (that is, the outer function) to its scope chain. Therefore, in the scope chain of the anonymous intrinsic function defined internally by the Createcomparisonfunction () function, it will actually contain the active object of the external function createcomparisonfunction ().
var compare = createComparisonFunction("name");var result = compare({name: "Nicholas"}, {name: "Greg"});
Shows the scope chain of the function and the internal anonymous function when the code executes.
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 (). In addition, when Createcomparisonfunction () executes, its active object is not destroyed, as you can see, the anonymous function also refers to the active object. when the createcomparisonfunction () function returns, the scope chain of its execution environment is destroyed, but its active objects remain in memory; After the anonymous function is destroyed, createcomparisonfunction () Will not be destroyed by the active object.
// 创建函数var compareNames = createComparisonFunction("name");// 调用函数var result = compareNames({name: "Nicholas"}, {name: "Greg"});// 解除对匿名函数的引用(释放内存)compareNames = null;
First, the comparison function that was created is saved in Comparenames, the comparenames is set to NULL, and the function's reference is lifted, it can be cleared.
Closures and variables
Closures save the entire variable object, but only the last value that contains any of the variables in the function.
Example:
function createFunctions() { var result = new Array(); for (var i = 0; i < 10; i++) { result[i] = function () { return i; }; } return result;}
In the example above, it seems that each function returns its own index value, in fact, each function returns 10. Because the active objects of the createfunctions () function are kept in the scope chain of each function, they refer to the same variable i.
Forcing the closure to behave as expected by creating another anonymous function, as follows:
function createFunctions() { for (var i = 0; i < 10; i++) { return [i] = function (num) { return function () { return num; }; }(i); } return result;}
This object
The This object is bound at run time based on the execution environment of the function, in the global function, this equals window, and when an object's method is called, this is equal to that object. However, the execution environment of an anonymous function is global, and its this object points to window.
See an example:
var name = "The Window";var object = { name: "My Object", getNameFunc: function () { return function () { return this.name; }; }};alert(object.getNameFunc()()); // "The Window"
The above example first creates a global variable name and creates an object that contains the Name property. This object contains a method: Getnamefunc (), which returns an anonymous function, and the anonymous function returns THIS.NAME. Because Getnamefunc () returns a function, calling Object.getnamefunc () () invokes the function it returns, with the result: THIS.name. However, the returned string is "the window".
Why does the anonymous function not get the This object that contains the scope?
Each function gets two variables when the function is called: this and arguments. When the intrinsic function searches for these two variables, it only searches for its active object, and it is not possible to access the two variables in the external function.
When you save the This object in an external scope to a variable that the closure can access, you can let the closure access the object.
var name = "The Window";var object = { name: "My Object", getNameFunc: function () { var that = this; return function () { return that.name; }; }};alert(object.getNameFunc()()); // "The Window"
JavaScript: Closures