- 作为普通函数
- 使用call,apply,bind
- 作为对象方法被调用
- 在class方法中调用
- 箭头函数
this是在函数执行的时候确认的,不是在函数定义的时候确认的
function fn1() {console.log(this)}fn1() // windoufn1.call({x: 100}) // {x: 100} //call 直接执行const fn2 = fn1.bind({x : 200}) //bind 会返回一个新的函数执行fn2() // {x: 200}
const zhangsan = {name: '张三',sayHi(){//this 指向当前对象console.log(this)}wait() {setTimeout(function() {// this === windouconsole.log(this)})}}
const zhangsan = {name: '张三',sayHi(){//this 指向当前对象console.log(this)}waitAgain() {setTimeout(() => {//this 指向当前对象console.log(this)})}}
class People {constructor(name) {this.name = name},sayHi(){console.log(this)}}const zhangsan = new People('张三')zhangsan.sayHi() //张三对象

