JS's closure

Source: Internet
Author: User

Reference: http://kingsmalltwo.iteye.com/blog/1962583

function A () {  var i=0;  Alert (' a ');  Function B () {    alert (++i);  }  return b;} var C = a (); C (); C ();

According to previous experience: Var c=a (), this sentence is to assign a function a () to C, but unfortunately in JS, is to return the value of B () assigned to C, here is the function of a ().

Summarize:

1, the outer layer of the closure is a function.

2, there are functions inside the closure.

3, the closure will return the intrinsic function.

4, there must be no return inside the function returned by the closure. (because it's really over.)

5, when the closure is executed, the inner variables of the closure are present, and the inner variables of the inner functions of the closure are not present.

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.