You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Person("Smith").sleepFirst(5).eat("supper");
// 输出:
// 等待5秒
// Wake up after 5
// Hi This is Smith!
// Eat supper
1.分析
1).链接所有 this;
2).把所有任务放到任务队列里面;
3).通过一个方法有序执行队列里面的任务;
2.实现
functionperson(name){returnnewPerson(name);}functionPerson(name){this.tasks=[];this.tasks.push(()=>{console.log('Hi!This is '+name+'.');this.then();});setTimeout(()=>{this.then();})}Person.prototype.eat=function(name){this.tasks.push(()=>{console.log('Eat '+name+'.');this.then();})returnthis;}Person.prototype.sleep=function(time){this.tasks.push(()=>{console.log('等待 '+time+'秒.');setTimeout(()=>{console.log('wake up after '+time+'seconds.');this.then();},time*1000)});returnthis;}Person.prototype.sleepFirst=function(time){this.tasks.unshift(()=>{console.log('等待 '+time+'秒.');setTimeout(()=>{console.log('wake up after '+time+'seconds.');this.then();},time*1000)});returnthis;}Person.prototype.then=function(){lettask=this.tasks.shift();task&&task();returnthis;}
1.分析
1).链接所有 this;
2).把所有任务放到任务队列里面;
3).通过一个方法有序执行队列里面的任务;
2.实现
3.测试
The text was updated successfully, but these errors were encountered: