1. Closed Package
Closures (closure) are a difficult and unique feature of the JavaScript language, and many advanced applications rely on closures.
Closures have three properties:
1. function nesting function;
2. External parameters and variables can be referenced inside the function;
3. Parameters and variables are not recycled by the garbage collection mechanism.
Closures are functions that have access to variables in another function scope, and the most common way to create closures is to create another function within one function and access the local variables of the function through another function. The use of closures has an advantage, but also its disadvantage, is that local variables can be resident in memory, you can avoid the use of global variables. Global variables can be called in every module, which is bound to be catastrophic. Therefore, it is recommended to use a private, encapsulated local variable. After the general function is executed, the local active object is destroyed and only the global scope is saved in memory. But the case of closures is different!
Example one:
Closure is the return value of a function is another function, outside the outer can be accessed through the return function of the local variables within the outer.
1 functionouter () {2 varval = 0;3 return function (){4Val + = 1;5document.write (val + "<br/>");6 };7 }8 varOutobj =outer ();9Outobj ();//1, after val + = 1 is executed, Val is still inTenOutobj ();//2 OneOutobj =NULL;//Val is recycled A varOutObj1 =outer (); -OutObj1 ();//1 -OutObj1 ();//2
Closures cause variables to be kept in memory at all times, and if used inappropriately increases memory consumption (if many outer () are defined in the previous example, many Val variables are saved in memory).
How JavaScript works with garbage collection:
(1), in JavaScript, if an object is no longer referenced, then the object will be recycled by GC;
(2) If two objects are referenced by each other and are no longer referenced by the 3rd, then the two objects referenced by each other are also recycled.
So what are the benefits of using closures? The benefits of using closures are:
1. Want a variable to be stationed in memory for a long time
2. Avoid contamination of global variables
3. Existence of Private members
2. Callback
callback function principle: I am leaving now, to inform you. " This is an asynchronous process, "I'm out" in the process (function execution), "You" can do anything, "to" (function Completion) "Notify You" (callback) after the process.
Example one:
1 function DoSomething (callback) {2 callback (); 3 }4function Numberadd (b) {5 document.write (A +b); 6 }7 dosomething (Numberadd); // 3
Example two:
function Thing (name) { this. Name = name;}
Add the DoSomething method to the Thing class, where the constructor invocation pattern is used
1Thing.prototype.doSomething =function(callback) {2Callback This. Name);3 };4 functionShowName (name) {5 document.write (name);6 }7 vart =NewThing ("Zhangsan");8T.dosomething (ShowName);//Zhangsan
If you have an array of numbers, you want to write a sort of public method, but the sort method (from small to large or from large to small) is the person who calls the sorting methods. Implementation of the sorting method can be implemented with callbacks, of course, you can write 2 methods, one is from small to large sort, one is from the large to the small sort method, just adopt the callback way to achieve simpler, more conducive to maintenance. The following example code is a typical use of callbacks:
vararr = [25,13,33,8,23,32]; Array.prototype.sort=function(callback) {vararr = This; vari = 0;//I define here with parentheses within the For loop (for (var i = 0; i < ...) ) definition is the same for(; i < arr.length-1; i++){ varj = i + 1; for(; J < arr.length;j++){ if(Callback (Arr[i],arr[j])) {vartemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; } } }returnarr;};//a-b>0 indicates that the array is sorted from small to largeArr.sort (function(A, b) {returnA-b > 0;}); document.write (Arr.join (",") + "<br/>");//8,13,23,25,32,33//b-a>0 indicates that the array is sorted from large to smallArr.sort (function(A, b) {returnB-a > 0;}); document.write (Arr.join (","));//33,32,25,23,13,8
JS Closures and callbacks