十三(11)、原型链方法中的this问题 ------ 2020

2020-04-06  本文已影响0人  自己写了自己看

1、原型链方法中的this判定三步骤:

/**
* 1、方法执行,看方法前面是否有点,点前面是谁,this就是谁;
* 2、把方法前面总的this进行替换;
* 3、再基于原型链查找的方法,确定结果即可;
*/
function Fn() {
    this.x = 100;
    this.y = 200;
    this.say = function () {
       console.log(this.x); 
   }
}

Fn.prototype.say = function () {
    console.log(this.y);
}

Fn.prototype.eat = function () {
   console.log(this.x + this.y);
}

let f1 = new Fn;

f1.say(); // this => f1; 100

f1.eat(); // 300

// 基于原型链先查找 eat 方法,但是点前面是谁,方法中的
// this就是谁,this => f1;

f1._proto_.say(); // this => f1._proto_ undefined

Fn.prototype.eat(); // this => Fn.prototype undefined

上一篇 下一篇

猜你喜欢

热点阅读