关于箭头函数 this的指向问题

2018-07-11  本文已影响0人  后来的猿

ES6 允许使用 “ 箭头 ” (=>)定义函数。

箭头函数 填 坑。

this的指向是 向上查找 非箭头函数的函数归属

this就指向这个归属

例如:

    function fn(){

        return ()=>{

            console.log(this)

        }

    }

    fn()()  //this =>>window

    function fn() {

        return function () {

            return () => {

                console.log(this)

            }

        }

    }

    fn()()() //this =>>window

function fn() {

var obj={

f:function (){

return ()=> {

console.log(this)

}

}

}

return obj

}

fn().f()() //this =>>f函数

上一篇 下一篇

猜你喜欢

热点阅读