Understanding JavaScript Closures

Source: Internet
Author: User

I don't know how to understand the concept of closures, I understand the closure is:

When a function A contains another function B,c calls function A, it is actually called function B, which is the closure (the inner function nested inside the function, the intrinsic function can access the local variables, arguments, and other intrinsic functions declared in the outer function, and the inner function is called by the external function ).

Let's look at an example:

function A () { //a contains the other functions B

var i=0;

Function B () {

Alert (++i)

};

Return b

};

var c=a (); when C calls function A, it actually calls function B.

alert (c); Function B () {Alert (++i)} is returned

Closures are actually the protection of internal variables,

But if these variables are not needed on the page, it can cause garbage.

will increase the memory.

C=null so I will be recycled.

What are the benefits of closures?

1) can exist its own private variables;

2) Avoid the influence of global variables;

3) Keep a variable stored in memory for a long time

Understanding JavaScript Closures

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.