function fn1(){
    console.log(this)
} 
fn1();//window

fn1.call({x:100});//{x:100}

const fn2 = fn1.bind({x:200})
fn2();//{x:200}
const zhangsan = {
    name:'张三',
    sayHi(){
        console.log(this);
    },
    wait(){
        setTimeout(function(){
            console.log(this)
        })

    }
}
zhangsan.sayHi();//zhangsan
zhangsan.wait()//window
const lisi = {
    name: '李四',
    sayHi() {
        console.log(this);
    },
    wait() {
        // 箭头函数取上级作用域的this,此处为wait里面的this,指向lisi
        setTimeout(()=> {
            console.log(this)
        })

    }
}
lisi.sayHi();//lisi
lisi.wait()//lisi

 

posted on 2020-03-20 16:53  chenlw101  阅读(73)  评论(0编辑  收藏  举报