Quick understanding of JS closure

Source: Internet
Author: User

/*
* 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

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.