手写备忘录
//防抖 const debounce = (fn,t)=>{ let timer = null; let delay = t || 1000; return function(){ let arg = arguments; if(timer){ clearTimeout(timer); } timer = setTimeout(()=>{ fn.apply(this,arg); },delay); } } //节流 const throttle = (fn,t)=>{ let timer = null; let delay = t || 1000; let initTime = null; return function(){ let arg = arguments; let now = Date.now(); if(initTime&&now-initTime<delay){ clearTimeout(timer); timer = setTimeout(()=>{ initTime = now; fn.apply(this,arg); },delay) }else{ initTime = now; fn.apply(this,arg) } } } //深拷贝 const deepClone = (obj) => { let result = Array.isArray(obj)?obj:{}; for(let key in obj){ let type = Object.prototype.toString.call(obj[key]); if(type=="[Object array]"||type=="[Object object]"){ result[key] = deepClone(obj[key]); }else{ result[key] = obj[key]; } } return result; } let test = (...args)=>{ console.log(args); } let a = debounce(test,500) test(1,2,3) //实现new const newFun = (fn,...args) => { let obj = Object.create(null); obj.__proto__ = fn.prototype; let res = fn.call(obj,...args); return res instanceof Object ? res : obj; } //flat const myFlat = (arr) => { let result = []; for(let i=0;i<arr.length;i++){ if(Array.isArray(arr[i])){ result = result.concat(myFlat(arr[i])); }else{ result.push(arr[i]); } } return result; } const myInstanceOf = (child,parent) => { const basicType = ["string","number","boolean","undefined"]; if(basicType.includes(typeof child)){ return true; } let parentPrototype = parent.prototype; let childProto = child.__proto__; while(true){ if(childProto==null){ return false; } if(childProto==parentPrototype){ return true; } childProto = childProto.__proto__; } } const Status = { Pending:"pending", Fullfilled:"fullfilled", Rejected:"rejected" } class MyPromise{ constructor(executor){ this.state = "pending"; this.value = ""; this.reason = ""; this.fullfilledCallback = []; this.reiectedCallback = []; const resolve = (value)=>{ if(this.state==Status.Pending){ this.state = Status.Fullfilled; this.value = value; this.fullfilledCallback.forEach(fn => { fn() }); } } const reject = (reason)=>{ if(this.state==Status.Pending){ this.state = Status.Rejected; this.reason = reason; this.reiectedCallback.forEach(fn => { fn() }); } } try { executor(resolve,reject) } catch (error) { reject(error) } } then(onFullFilled,onRejected){ return new MyPromise((resolve,reject)=>{ let x; if(this.state==Status.Fullfilled){ x = onFullFilled(this.value); resolve(x) } if(this.state==Status.Rejected){ x = onRejected(this.reason); reject(x) } if(this.state==Status.Pending){ this.fullfilledCallback.push(()=>{ x = onFullFilled(this.value); resolve(x) }) this.reiectedCallback.push(()=>{ x = onRejected(this.reason); resolve(x) }) } }) } } const p = new MyPromise((resolve,reject)=>{ // let success = true; // let value = 1; // if(success){ // resolve(value) // }else{ // reject("error") // } setTimeout(()=>{ reject("error"); },1000) }) p.then((res)=>{ console.log(res) },(err)=>{ console.log(err) }) p.then((res)=>{ console.log(res) },(err)=>{ console.log(err) }) Function.prototype.myCall = function(){ let context = arguments[0]||window; context.fn = this; let param = []; for(let i=1;i<arguments.length;i++){ param.push(arguments[i]); } let result = context.fn(...param); return result; } const obj1 = { a:1, fn:function(){ console.log(this.a) } } const obj2 = { a:2 } let f = obj1.fn.myCall(obj2); console.log(f); //归并排序 function mergeSort(arr){ var merge = function(leftArr,rightArr){ var arrTemp = []; while(leftArr.length&&rightArr.length){ arrTemp.push(leftArr[0]<rightArr[0]?leftArr.shift(0):rightArr.shift(0)); } return Array.prototype.concat.call(arrTemp,leftArr,rightArr); } if(arr.length<2){ return arr; } var mid = parseInt(arr.length/2); return merge(mergeSort(arr.slice(0,mid)),mergeSort(arr.slice(mid))); } var msarr = mergeSort([5,9,3,6,7,8,3]); console.log(msarr) //emitEvent class EmitEvent{ constructor(){ this._event = {} } on(eventName,cb){ var query = this._event[eventName]; this._event[eventName] = query?query.push(cb):[cb]; return this; } emit(eventName,query){ this._event[eventName].forEach((fn)=>{ fn.call(this,query) }) return this; } off(eventName,cb){ var query = this._event[eventName]; this._event[eventName] = query&&query.filter(fn=>fn!=cb); return this; } once(eventName,cb){ var onceFun = (...args)=>{ cb.apply(this,args) this.off(eventName,onceFun) } this.on(eventName,onceFun) return this; } } const _quickSort = array => { // 补全代码 if(array.length<2){ return array; } let mid = parseInt(array.length/2); let left = [], right = []; for(let i=0;i<array.length;i++){ if(i==mid){ continue; } if(array[i]<=array[mid]){ left.push(array[i]) }else{ right.push(array[i]) } } return [..._quickSort(left),array[mid],..._quickSort(right)] } console.log(_quickSort([0,-1,1,-2,2])) //全排列,回溯,动态规划 function permute(nums){ let len = nums.length,res = []; if(!len) return res; let path = [],used = []; deps(nums,len,0,path,used,res); return res; function deps(nums,len,depth,path,used,res){ if(depth == len){ res.push([...path].join('')); return ; } for(let i=0;i<len;i++){ if(!used[i]){ path.push(nums[i]); used[i] = true; console.log(188,path); console.log(189,used); deps(nums,len,depth+1,path,used,res); used[i] = false; path.pop(); console.log(194,path); console.log(195,used); } } } } console.log("quan:"+permute([1,2,3]))