站稳脚跟 2022-05-04 13:56:38
function LazyMan(name) { if (!(this instanceof LazyMan)) { return new LazyMan(name); } this.name = name; this.delayTime = 0; this.tasks = []; console.log(`Hi I am ${this.name}`); this._ready(); } LazyMan.prototype._ready = function () { setTimeout(() => { setTimeout(() => { console.log(`等待了${this.delayTime}秒...`); this._doTasks(); }, this.delayTime * 1000); }, 0); } LazyMan.prototype._doTasks = function () { const tasks = this.tasks; if (tasks.length === 0) return; const {delayTime, callback} = tasks[0]; setTimeout(() => { callback && callback(); tasks.shift(); this._doTasks(); }, delayTime); } LazyMan.prototype.eat = function (foods) { this.tasks.push({ delayTime: 0, callback() { console.log(`I am eating ${foods}`); }, }); return this; } LazyMan.prototype.sleep = function (seconds) { if (seconds >= 0) { this.tasks.push({ delayTime: seconds * 1000, callback() { console.log(`等待了${seconds}秒...`) } }); } return this; } LazyMan.prototype.sleepFirst = function (seconds) { if (seconds > 0) { this.delayTime = seconds; } return this; }
- 共 1 页
- 1
2个问题:1、字符串数组;2、没去重。修正:Array.from(new Set(arr.toString().split(",").map(Number))).sort((a,b)=>{ return a-b})
第 11 题:将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组