JavaScript closures (dog blood plot, easy to understand)

Source: Internet
Author: User

Let's start by looking at a closure function:

function A () {            var i = 0;             function B () {                alert (+ +i)            ;            } return b;        }         var c = A ();        C ();        C ();        C ();

Equivalent to

function Male 1 () {            var child = 0;             function Female 1 () {                alert (+ + children )            ;            } return female 1;        }         var male 2 = male 1 ();        Male 2 ();        Male 2 ();        Male 2 ();

So then we analyze and analyze the plot of the dog's blood.

JavaScript rule : If only two people refer to each other, then the GC (Marriage Registry) will recycle this two people (PS: Province of them mess!) )。

closure: It is three people, you have me, I have him, he has you, so complex relationship, GC is not bothered to bother the tube.

Plot:

1, male 1 Heart (scope chain scope) Female 1, female 1 belly (scope chani scope of arguments) have children male 1 and female 1 relationship

PS: If only male 1 and female 1, then it conforms to the rules of JavaScript, directly to the happy life

2, The evil Men 2 debut, male 2 also like female 1, and tell the male 1 you give her to me, male 1 really to the male 2

PS: Strongly condemn male 2 and male 1, the biggest problem is that female 1 already has a male 1 child

3, male 2 called the female 1, actually gave birth to a male 1 child

PS: The above plot true dog blood, so closures, not special environment do not use, hehe ~ ~

JavaScript closures (dog blood plot, easy to understand)

Related Article

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.