class HardMan {constructor(name) {this.name = name;this.taskQueue = Promise.resolve();//考察执行栈和事件循环 setTimeout(() => {this.execute();})}execute() {if (this._restFirst) {this.realRest(this._restFirst);this._restFirst = 0;}this.outName();if (this._rest) {this.realRest(this._rest)}if (this._learn) {this.taskQueue = this.taskQueue.then(() => {console.log(`Learning ${this._learn}`)});}}outName() {this.taskQueue = this.taskQueue.then(() => {console.log(`I am ${this.name}`)});return this;}rest(seconds) {this._rest = seconds;return this;}realRest(seconds) {this.taskQueue = this.taskQueue.then(() => { return new Promise(resolve => {setTimeout(() => {console.log(`After rest ${seconds} seconds`);resolve();}, seconds * 10);});});return this;}restFirst(seconds) {this._restFirst = seconds;return this;}learn(subject) {this._learn = subject;return this;}}

相关推荐

看起来名字可以很长:笑死 我暑期实习阿里云的意向也被 qq 邮箱放在垃圾箱了
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务