function eat(x, y) {
console.log(x + y);
console.log(this);
}function drink(x, y) {
console.log(x - y);
console.log(this);
} eat.call(drink, 3, 2);输出结果为什么是
drink 中的语句一条也不会执行吗?那为什么 this指向了drink?还做了一些测试如下: function eat() {
this.x = 1;
this.y = 2;
// this.z = this.x + this.y;
console.log(this.z);
} function drink() {
this.x = 2;
this.y = 3;
this.z = this.x + this.y; console.log(this);
} eat.call(drink);
var d = new drink();
eat.call(d)
测试一:测试二:
两次测试中代码区别只有eat中this.z有没有被赋值测试结果怎么理解?
函数.call(对象)的作用只是:执行函数,并且把call()第一个参数的对象赋值给函数内的this。仅此而已。