function lyc(name){
this.task = [];
let fn = () => {
console.log(`I am ${name}`);
this.next();
}
this.task.push(fn);
setTimeout(() => {
this.next();
}, 0)
return this;
}
lyc.prototype.restFirst = function (timer) {
let fn = () => {
setTimeout(() => {
console.log(`Start learning after ${timer} seconds`);
this.next();
}, timer * 1000)
}
this.task.unshift(fn);
return this;
}
lyc.prototype.rest = function (timer) {
let fn = () => {
setTimeout(() => {
console.log(`Start learning after ${timer} seconds`);
this.next();
}, timer * 1000)
}
this.task.push(fn);
return this;
}
lyc.prototype.learn = function (nationality) {
let fn = () => {
console.log(`Learning ${nationality}`);
this.next();
}
this.task.push(fn);
return this;
}
lyc.prototype.next = function () {
let fn = this.task.shift();
fn && fn()
}
// console.log(new lyc("john"))
// console.log(new lyc("john").rest(2).learn("chinese"))
// console.log(new lyc("john").restFirst(2).learn("chinese"))
console