fn2.num 为啥打印的是111,f1中的this经过call指向了f2,为啥去打印f1的num?
function fn1() {
console.log(1);
this.num = 111;
this.sayHey = function() {
console.log("say hey.");
}
}
function fn2() {
console.log(2);
this.num = 222;
this.sayHello = function() {
console.log("say hello.");
}
}
fn1.call(fn2);
console.log(fn2.num); // 111
fn1.call(fn2),执行的程序是fn1的内容,但this是fn2,所以相当于执行了下面的代码,也就是fn1里面的this替换为fn2
显然, console.log(fn2.num)输出111