Read someone else's blog and jot it down
Let's take a look at the following code
1 var a=1,b=2,c=3; 2 3 (function() {4 var a=b=1; 5 })(); 6 7 Console.log (a); 8 Console.log (b);
This is a well-known closure, yes, a very important use of closures is that variables can not be accessed externally, will not pollute the external variables , so you would think that the results of 7,8 rows are 1 2? Otherwise, 7, 8 rows of the correct result is 1, 1.
Why is that? I also think the result is 1 2, in fact, JS is executed, in the closure of VAR declaration is a local variable, so a is a local variable, and B, B isnot declared with Var, so in the closure of B is a global variable , so the front of the b=2 is closed inside the b=1 covered, So the outputs A and B are all 1 .
That's how I understand it. We have different understanding can leave a message, because the first time to write a blog, poor writing, we forgive
Small discovery of Var a=b=c=d in JavaScript