<分区>
在下面的示例中,当我运行此代码时,调用启动函数时,仅在 _one
函数中定义了 this
。当继续执行下一个函数 _two
时,this
未定义。有什么解释吗?以及如何解决这个问题?
提前致谢。
'use strict';
class MyClass {
constructor(num) {
this.num = num;
}
start() {
this._one()
.then(this._two)
.then(this._three)
.then(this._four)
.catch((err) => {
console.log(err.message);
});
}
_one() {
console.log('num: ' + this.num);
return new Promise((resolve, reject) => {
resolve();
});
}
_two() {
console.log('num: ' + this.num);
return new Promise((resolve, reject) => {
resolve();
});
}
_three() {
console.log('num: ' + this.num);
return new Promise((resolve, reject) => {
resolve();
});
}
_four() {
console.log('num: ' + this.num);
return new Promise((resolve, reject) => {
resolve();
});
}
}
let myClass = new MyClass(4);
myClass.start();