时刻准备着收集一些常见的手写题(上)
Tom防抖
触发高频事件n秒内函数只执行一次,如果n秒内高频事件再次被触发,则重新计算时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
function debounce(fn, daley, immediate){ let timer return function () { if (timer) clearTimeout(timer) if (immediate) { let run = !timer timer = setTimeout(() => { timer = null }, daley) if (run) { fn.apply(this, arguments) } } else { timer = setTimeout(() => { fn.apply(this, arguments) }) } } }
let box = document.getElementById('box')
function say(){ console.log('hi debounce') } box.addEventListener('input',debounce(say,1000,true))
|
节流
高频事件触发,但在n秒内只会执行一次,节流会稀释函数的执行频率
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| function throttle(fn,daley){ let canRun = true return function(){ if(!canRun) return canRun = false setTimeout(()=>{ fn.apply(this,arguments) canRun = true },daley) } }
let box = document.getElementById('box')
function say() { console.log('hi throttle') } box.addEventListener('input', throttle(say, 1000))
|
深拷贝
ES6中的结构赋值和Object.assign都是浅拷贝(一层的话看不出来)
1 2 3 4 5 6 7 8 9 10 11
| function deepClone(obj){ if(obj == null) return obj; if(obj instanceof Date) return new Date(obj); if(obj instanceof RegExp) return new RegExp(obj); if (typeof obj !='object') return obj; let newObj = new obj.constructor; for(let key in obj){ newObj[key] = deepClone(obj[key]); } return newObj }
|
不能解析new Date(), reg: /reg/, fn: function () {} 需要单独处理
发布订阅模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| let event = { list: {}, on(key, fn) { if (!this.list[key]) { this.list[key] = []; } this.list[key].push(fn); }, emit() { let key = [].shift.call(arguments), fns = this.list[key];
if (!fns || fns.length === 0) { return false; } fns.forEach(fn => { fn.apply(this, arguments); }); }, remove(key, fn) { let fns = this.list[key]; if (!fns) return false; if (!fn) { fns && (fns.length = 0); } else { fns.forEach((cb, i) => { console.log(cb,fn,cb === fn) if (cb === fn) { fns.splice(i, 1); } }); } } };
function cat() { console.log('一起喵喵喵'); } function dog() { console.log('一起旺旺旺'); }
event.on('pet', data => { console.log('接收数据'); console.log(data); }); event.on('pet', cat); event.on('pet', dog);
event.remove('pet', dog);
event.emit('pet', ['二哈', '波斯猫']);
|