老师我想问一下为什么我这么写promise的构造器时,为什么resolve方法内部的this为undefined
class MyPromise{
state = 'pending'
value = null
reason = null
rejectCallbacks = []
resolveCallbacks = []
constructor(excutor){
try{
excutor(this.resolve,this.reject)
}catch(error){
this.reject(error)
}
}
resolve(value){
console.log('resolve:',this) //undefined
if(this.state ==='pending'){
this.value = value
this.state = 'resolved'
while(this.resolveCallbacks.length){
this.resolveCallbacks.shift()(this.value)
}
}
}
但是下面这样写的话,方法中的this就是实例对象
class person{
constructor(){
this.name = 'ohh'
}
say(){
console.log(this.name) //this为实例对象
}
}
请问为什么同样是在类方法中使用this,但是一个是undefined,一个是实例对象?