A brief discussion of JavaScript closure _javascript skills based on a piece of code

Source: Internet
Author: User
Tags closure
Copy Code code as follows:

Function F1 () {
var n = 999;
Nadd = function () {n + = 1;}
function F2 () {
alert (n);
}
return F2;
}

The closure here is F1, enclosing a variable n and a function F2.

Let's just ignore nadd and try to keep it as it is. Rewrite the function.
Copy Code code as follows:

Function F1 () {
var n = 999;
var F2 = function () {alert (n);};
return F2;
}
var result = F1 ();
Result ();

The variables in JS are encapsulated by function, and when a variable is not found inside the function, the function looks in the previous unit (context) where it is located, and finds the Window field to the top level.
Then there is a question: Does the lookup process start with the location of the function reference or the position defined by the function body?
In this piece of code, the field of result is window, but the actual output is the N value inside the F1, so it can be concluded that the starting point of the variable lookup is the position defined by the function body.

Now look back at Nadd (the first code). As we know, the variable with no keyword var definition enters the window field by default, so Nadd is actually window.nadd. This is equivalent to the following code:
Copy Code code as follows:

var Nadd;
Function F1 () {
var n = 999;
Nadd = function () {n + = 1;}
function F2 () {
alert (n);
}
return function () {alert (n);};
}

So according to our analysis of result, the execution of Nadd will affect the value of N in F1.
So there are:
Copy Code code as follows:

Function F1 () {
var n = 999;
Nadd = function () {n + = 1;}
function F2 () {
alert (n);
}
return function () {alert (n);};
}
var result = F1 ();
Result ();
Nadd ();
Result ();

This code performs the final output of 1000.

Look at this situation again:
Copy Code code as follows:

Function F1 () {
var n = 999;
Nadd = function () {n + = 1;}
function F2 () {
alert (n);
}
return function () {alert (n);};
}

F1 () (); <--p1
Nadd ();
F1 () (); <--p2

Briefly describe the implementation process:
P1 position, F1 encapsulates an anonymous closure a, and then executes the A:F2,A:F2 output variable a:n after returning the function a:f2 in a closure, and the result is 999.
At the same time, the Nadd is assigned a function in the a closure, and the next line executes Nadd that gives the a:n a value of +1.
P2 location, F1 package anonymous closure B, the operation is for the closure B, and then perform B:F2 output is b:n, so the final result is still 999.
A and B are two separate "packages" that do not affect each other.

Rewrite the call section of the function:

Copy Code code as follows:

Function F1 () {
var n = 999;
Nadd = function () {n + = 1;}
function F2 () {
alert (n);
}
return function () {alert (n);};
}

var result = F1 ();
Result ();
Nadd ();
F1 () ();
Result (); <--p3

The P3 position is not accidentally exported to 1000.

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.