我不明白这个Javascript?

时间:2017-05-08 04:10:09

标签: javascript

有人可以通过这个例子走我,为什么打印10?我觉得在功能方面的功能让我感到困惑。另外var x =x =之间有什么区别?

var x = 6;
var y = 4;
var a = function(b) {
  return function(c) {
    return y + b + c;
  }
};
x = 2;
y = 5;
var fn = a(x);
x = 1;
y = 3;
var unknown = 5;
console.log(fn(unknown));

1 个答案:

答案 0 :(得分:1)

var x = 6; 
var y = 4; 
var a = function(b) {
    return function(c) {
        return y + b + c;
    }
};
x = 2; 
y = 5; // unnecessary, meant to confuse
var fn = a(x); // creates a closure, where b equals 2
x = 1; // unnecessary, meant to confuse
y = 3; 
var unknown = 5;
console.log(fn(unknown)); // y=3,b=2,c=5, total = 10