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)