眉目亦如画i 2022-05-04 13:57:31
补充一点:类继承是单一继承结构,只有一个父类;而原型继承本质上是组合,它可以有多个父类,且不会产生层级分类这样的副作用。
眉目亦如画i 2022-05-04 13:56:24
const reverseCase = str => { let s = ''; for(let i of str){ if(i === i.toUpperCase()){ s += i.toLowerCase(); }else { s += i.toUpperCase(); } } return s; }; reverseCase('abCDeFgHe'); // "ABcdEfGhE"
眉目亦如画i 2022-05-04 13:54:20
class MutiRequest { constructor(urls, max) { this.urls = urls this.max = max this.pending = [] this.excuting = [] this.result = [] this.init(urls) } init(urls) { for (let i = 0; i < urls.length; i++) { this.add(fetchApi(urls[i]), i) } } add(promiseFn, index) { if (this.excuting.length === this.max) { return new Promise((resolve) => { this.pending.push(() => { this.excute(promiseFn, resolve) }) }).then((data) => { console.log(index, data) this.result[index] = data this.printOut() }) } else { return new Promise((resolve) => { this.excute(promiseFn, resolve) }).then((data) => { console.log(index, data) this.result[index] = data this.printOut() }) } } printOut () { if (this.result.length === this.urls.length) { console.log(this.result) } } excute(promiseFn, resolve) { this.excuting.push(promiseFn) promiseFn.then((data) => { resolve(data) this.excuting = this.excuting.filter((item) => { return item !== promiseFn }) if (this.pending.length) { this.pending.shift()() } }) } } // 模拟请求 const fetchApi = (url) => { return new Promise((resolve, reject) => { let random = Math.random() * 10 setTimeout(() => { resolve('res:' + url) }, 1000 * random) }); } // 测试 let mutiTest = new MutiRequest([ 'https://api.xx.com/01', 'https://api.xx.com/02', 'https://api.xx.com/03', 'https://api.xx.com/04', 'https://api.xx.com/05', 'https://api.xx.com/06', 'https://api.xx.com/07', 'https://api.xx.com/08', 'https://api.xx.com/09', 'https://api.xx.com/10', 'https://api.xx.com/11', 'https://api.xx.com/12', 'https://api.xx.com/13', 'https://api.xx.com/14', 'https://api.xx.com/15', ], 2)
- 共 1 页
- 1
第 11 题:将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组