this指向问题

1、对象内函数的this指向对象

    let obj = {
        name: 'obj',
        f: function () {
            return this
        }
    }
    console.log(obj.f())

2、全局函数指向window

    let f = function () {
        return this
    }
    console.log(f())

3、箭头函数指向其所在外层对象的this

    let obj = {
        name: 'obj',
        fs: null
    }
    let fs = () => {
        return this
    }
    obj.fs = fs
    console.log(fs())
    console.log(obj.fs())

4、对象内函数返回的嵌套函数this指向window

    let obj = {
        name: 'obj',
        f: function () {
            return function fs() {
                return this
            }
        }
    }

    console.log(obj.f()())
posted @ 2021-10-26 17:55  陈大雷Q  阅读(28)  评论(0)    收藏  举报