Here are the codes:
function fn() {
var x = 1;
function fn2() {
x++;
console.log(x);
}
return fn2;
}
var foo = fn();
var bar = fn();
foo(); //2
bar(); //2
foo(); //3
I got a problem that I can't figure out why the result is not 2 3 4.I mean,according the closure principle,foo and bar should have maintain the scope of the function fn,so I thought foo and bar have the same x.Hope you can help me solve the problem.