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