This article analyzes the JS scope closure, the pre interpretation and this keyword. Share to everyone for your reference, specific as follows:
var number = 2;
var obj = {number:5,
fn1: (function () {
this.number *= 2;
number=number*2;
var number=3;
return function () {
this.number *= 2;
number*=3;
alert (number);
}
)()
};
var fn1 = obj.fn1;
alert (number);
FN1 ();
Obj.fn1 ();
alert (window.number);
alert (Obj.number);
Resolution
1. FN1 itself is followed by (), so var fn1 = obj.fn1; He was executed, but the return function inside was not executed.
2. Alert (number), the output of the global number, is originally 2, after the var fn1 = obj.fn1;
This.number *= 2;
number=number*2;
var number=3;
These three words
Note: Any anonymous method executed directly, his this point to window
So This.number *= 2, so that the global variable becomes 4, which is output 4.
and number=number*2; He was going to see the Number,var number=3 in the scope block; The scope declaration was first, but there was no assignment, so number=number*2; it doesn't really work.
3. Execution to FN1 () is the execution of the return function inside the FN1, which is the three words
This.number *= 2;
number*=3;
alert (number);
This is still an anonymous method, so This.number *= 2; makes the global variable 8,number*=3; To find number in the scope block, the outer definition number is 3, so alert (number) is 9, if alert ( This.number); point to global variable, that is, output 8
4. OBJ.FN1 (), still executes the return function inside the FN1, which is the three words
This.number *= 2;
number*=3;
alert (number);
But then this point to Obj,this.number *= 2, make the number in obj into 10,number*=3, or to find the scope block, because the above becomes 9, so this is 27,alert (number); The output is number of the scope block, that is, 27, if alert (this.number), point to obj number, that is, output 10
5. Alert (window.number); After the previous rounds, the global variable becomes 8 (that is, execution obj.fn1 (); no effect on global variables)
6. Alert (obj.number); only this sentence obj.fn1 ();, changed obj.number, so output 10
(function () {
var a=10;
fn ();
function fn () {
var a=a+10;
Console.log (a);
return A;
}
Console.log (a);
Console.log (FN () +10);
}) ();
A in the FN function is first declared but not assigned, and then the operation is performed, and he does not look for a variable with the same name outside the function because he is already declared inside. An operation that is not numeric and numeric, outputting nan
Console.log (FN () +10); This sentence outputs two values: Console.log (FN ()) and Console.log (FN () +10);
Results:
NaN
10
NaN
NaN
If the title is changed into
(function () {
var a=10;
fn ();
function fn () {
a=a+10;
Console.log (a);
return A;
}
Console.log (a);
Console.log (FN () +10);
}) ();
Results:
20
20
30
40
More readers interested in JavaScript-related content can view the site topics: "javascript array Operation tips Summary", "JavaScript Sorting algorithm Summary", "JavaScript traversal algorithm and Skills summary", " JavaScript Mathematical Operational Usage Summary, JavaScript data structure and algorithm skills summary, JavaScript Search algorithm Skills summary and JavaScript error and Debugging skills summary
I hope this article will help you with JavaScript programming.