简兴瑶

Results 16 comments of 简兴瑶

``` javascript // 观察者模式 // 观察者 class Observer{ _map = new Map() constructor(){ } notify(type,message){ let subs = this._map.get(type) if(subs){ subs.forEach(sub => sub.update(message)) } } depend(type,sub){ let subs = this._map.get(type)...

``` javascript function request(url,maxTime){ return new Promise((resolve,reject) => { const controller = new AbortController() fetch(url,{signal:controller.signal}).then(res => { resolve(res) }) setTimeout(() => { controller.abort() reject(new Error('超时了')) },maxTime) }) } ```

``` javascript // 组合继承 function parentClass(name){ this.name = name; } function childClass(name,age){ parentClass.call(this,name); this.age = age; } childClass.prototype = Object.create(parentClass.prototype); childClass.prototype.constructor = childClass; // 寄生组合式继承 function inherit(parent,child){ const fn =...

type MyPick = { [K in U]: T[K]; }; 加了点小约束 pick 主要就是只保留传入的键类型

``` javascript function clone(target,deep = true,map = new WeakMap()){ if(typeof target !== 'object' || target === null){ return target } else if(typeof target === 'function'){ if(!deep){ return target } return...

``` type MyReadonly = { readonly [K in keyof T]:T[K] extends Record ? MyReadonly : T[K]; } 一个递归 主要在于判断是不是对象形式的类型 如果是就递归 ```

``` javascript // promise 红绿灯 const task = (time, type) => { return new Promise(res => { setTimeout(() => { if (type === 'green') { console.log('绿灯') } else if (type...

``` javascript class Scheduler { // 当前任务执行数量 curCount = 0; // 阻塞任务 queue = []; constructor(max){ this.max = max } async add(cb){ if(this.curCount >= this.max){ await new Promise(resolve => this.queue.push(resolve))...

``` javascript '{{1}}222{{3}}'.replace(/({{\w*}})/g,m => { console.log(m) }) ```

> ```js > '{{1}}222{{3}}'.replace(/({{\w*}})/g,m => { > console.log(m) > }) > ``` 没懂题目啥意思 单纯匹配模板字符串?