箭頭函數的this

定義時所處的對象就是它的this
看外層是否有函數
若是有,外層函數的this就是內部箭頭函數的this
若是沒有,this就是window
let obj = {
    name : '箭頭函數',
    getName : () => {
        setTimeout(() => {

            // 這裏this指向window
            // 能夠理解成下面這種形式
            console.log(this);
        }, 10);
    }
};

// 指向window的緣由
// obj.getName = () => {
//     console.log(this);
//     setTimeout(() => {
//         console.log(this);
//     }, 10);
// }
// obj.getName();
相關文章
相關標籤/搜索