整理一下前端面试中常考的一些函数
1 /** 2 * promiseAll 3 * author yh 4 * to make many promise and finished together ,return the result that the promise 5 * 6 **/ 7 function promiseAll(promiseArray){ 8 return new Promise((resolve, reject) => { 9 if(!Array.isArray(promiseArray)) return reject(new Error('入参是数组')) 10 const result = []; // to save the Promise value 11 const len = promiseArray.length; 12 let couter = 0; 13 for(let i=0; i<len; i++ ){ 14 Promise.resolve(promiseArray[i]).then(value => { 15 couter++; 16 result[i] = value; 17 if(counter===len) resolve(res) 18 }).catch(e => reject(e)) 19 } 20 }) 21 } 22 /** 23 * new 24 * author yh 25 * 26 **/ 27 function news(f){ 28 return function () { 29 var obj = { "__proto__": f.prototype } 30 f.apply(obj, arguments); 31 return obj 32 } 33 } 34 function news(f){ 35 return function () { 36 var obj = {'__proto__': f.prototype }; 37 f.apply(obj,arguments); 38 return obj 39 } 40 } 41 /** 42 * deppClone 43 * author yh 44 * @param {*} object,number,boolean,string,NULL ,undefined 45 */ 46 function deepClone(target){ 47 if(typeof target !=='object') return target; 48 let obj = Array.isArray(target)?[]:{}; 49 for(let j in target){ 50 if(target.hasOwnProperty(j)){ 51 if(typeof target ==='object'){ 52 obj[i] = deepClone(target[j]) 53 }else{ 54 obj[i] = target[i] 55 } 56 } 57 } 58 return obj 59 } 60 /** 61 * 62 * @param {*} arr 63 * @returns 64 */ 65 66 /** 67 * quickSort 68 * author yh 69 * @param {*} arr 70 **/ 71 function quickSort(arr){ 72 if(!Array.isArray(arr)) return new Error('quickSort的入参必须是数组'); 73 let result = arr.sort((a,b)=> {return a-b }) 74 return result; 75 } 76 77 /** 78 * solution 79 * author yh 80 * get the median of the array 81 */ 82 function solution(arr){ 83 if(!Array.isArray(arr)) return new Error('solution的入参必须是数组'); 84 let result = arr.sort((a,b) => { return a-b }); 85 let len = result.length; 86 let mid = 0; 87 if(len%2===0){ 88 mid = (result[len/2] +result[len/2+1])/2 89 }else{ 90 mid = result[(len+1)/2] 91 } 92 return mid 93 } 94 95 /** 96 * 订阅者和发布者实现 97 * 98 * */ 99 let event = { 100 clientList:{}, 101 listen:function(key,fn){ 102 if(!this.clientList[key]){ 103 this.clientList[key] = [] 104 } 105 this.clientList[key].push(fn) 106 }, 107 trigger:function(){ 108 var key = Array.prototype.shift.call(arguments), 109 fns = this.clientList[key] 110 if(!fns||fns.length==0) return false 111 for(let i =0;fn;fn=fns[1++]){ 112 fn.apply(this,arguments) 113 } 114 }, 115 remove:function(key,fn){ 116 let fns = this.clientList[key] 117 if(!fns) return false 118 if(!fn){ 119 fns&&(fns.length===0) 120 }else{ 121 for(let i=0;i<fns.length;i++){ 122 var _fn = fns[i]; 123 if(_fn===fn) fns.splice(i,1) 124 } 125 } 126 } 127 } 128 129 let installEvent = function(obj){ 130 for(let i in event){ 131 obj[i] = event[i] 132 } 133 } 134 135 136 /** 137 * 防抖实现 138 * debounce 139 * @author yh 140 * @params fn, interval, immediate 141 */ 142 function debounce(fn,interval,immediate){ 143 let timer = null; 144 return function(){ 145 let context = this; 146 let callNow = !timer&&immediate; 147 clearTimeout(timer); 148 timer=setTimeout(function(){ 149 !timer&&fn.apply(context,args); 150 timer = null 151 },interval) 152 callNow&&fn.apply(context,args) 153 } 154 } 155 /** 156 * 截流函数 157 * throttle 158 * @author yh 159 * @params fn:函数,delay:延迟时间 160 */ 161 function throttle(fn,delay){ 162 let timer = null; 163 let startTime = Date.now(); 164 return function(){ 165 let curTime = Date.now(); 166 let remaining = delay - (curTime-startTime); 167 let context = this; 168 let args = arguments; 169 clearTimeout(timer); 170 if(remaining<=0){ 171 fn.apply(context,args); 172 startTime = Date.now() 173 }else{ 174 timer= setTimeout(function(){ 175 fn.apply(context,args); 176 timer = null; 177 },delay) 178 } 179 } 180 } 181 182 /** 183 * 函数柯里花 184 * curry 185 */ 186 const curry = fn =>{ 187 if(fn.length <= 1) return fn; 188 const generator = args => { 189 args.length === fn.length?fn(...args): arg => generator([...args,arg]); 190 } 191 return generator([],fn.length); 192 } 193 /** 194 * js无限累加函数 195 * add 196 * 197 */ 198 function add(a){ 199 function sum(b){ 200 a = a+b; 201 return sum 202 } 203 sum.toString = function(){ 204 return a 205 } 206 return sum; 207 } 208 /** 209 * getParams 210 * 211 */ 212 function getParams(){ 213 let query = location.search.substr(1); 214 if(!query) return {} 215 let arr = query.split('&') 216 let obj = {} 217 for(let i=0;i<arr.length;i++){ 218 let keyword = arr[i].split('='); 219 obj[keyword[0]]=keyword[1] 220 } 221 return obj 222 }
优秀的人一直在路上,优秀的你一直在尝试写新bug