/*
* Closures: 1. Change the value of a variable inside a function without affecting the outside of the function global variable (equivalent to a private variable in Java)
* 2. After the closure is called, the resulting variable value is not released.
* 3. If anyone calls a closure, the values inside the closure do not affect each other and are the values originally defined.
*/
Example explanation 1:
var name= ' window ';
var qinbb={
Name: ' Qinbb ',
Getname:function () {
return function () {
Console.log (this);//function This refers to the Window {external:object, Chrome:object, Document:document, Qinbb:object, SPEECHSYNTHESIS:SPEEC Hsynthesis ...}
return this.name;//This refers to the this of return function (), so it is the global variable var name= ' window ';
};
}
};
Qinbb.getname () ();//output: ' Window ' method call is added after the method name (), if there is a method inside the method will be added () at the back;
Example 2, 3:
function A () {
var n=1;
Function B () {
n++;
Return alert (n);
}
return B ();//if return B;
}
var c=a ();//Output 2
var d=a ();//Output 2
//if return B;
var c=a ();
C ();//Output 2 method call added after the method name ()
Quick understanding of JS closure