鍍金池/ 問(wèn)答/HTML/ 箭頭函數(shù)定義對(duì)象方法時(shí)的this取值為何不是對(duì)象本身?

箭頭函數(shù)定義對(duì)象方法時(shí)的this取值為何不是對(duì)象本身?

function Person() {
    this.name = 'ie'
};
Person.prototype.sayName = () => {
        console.log(this.name);
}

編譯結(jié)果

var _this = this;
function Person() {
    this.name = 'ie';
}
;
Person.prototype.sayName = function () {
    console.log(_this.name);
};
回答
編輯回答
空白格

因?yàn)榧^函數(shù)本身沒(méi)有this,它是根據(jù)當(dāng)前的作用域鏈向上查找this的指向,你的題目當(dāng)前this的指向是Window對(duì)象,所以才會(huì)出現(xiàn)最后的編譯結(jié)果

2017年11月4日 17:08
編輯回答
維他命

沒(méi)有為什么,規(guī)范就是這樣規(guī)定的 箭頭函數(shù)體內(nèi)的this對(duì)象,就是定義時(shí)所在的對(duì)象,而不是使用時(shí)所在的對(duì)象。

2017年3月14日 00:09