本文主要记录前端笔试中经常考到的基础手写题。防抖节流://防抖functiondebounce(fn,time){lettimer;返回函数(){clearTimeout(计时器);timer=setTimeout(()=>{fn.call(this,...arguments);},time);};}//节流函数throttle(fn,delay){varprev=Date.now();返回函数(){varnow=Date.now();if(now-prev>=delay){fn.call(this,...arguments);prev=Date.now();}};}继承://组合继承functionParent(name){this.name=name;}Parent.prototype.getName=function(){console.log(this.name)}functionChild(name){Parent.call(this,name);}Child.prototype=newParent();//寄生组合functionParent(name){this.name=name;}Parent.prototype.getName=function(){console.log(this.name)}functionChild(name){Parent.call(this,name);}Child.prototype=Object.create(Parent.prototype,{constructor:{value:Child,writable:true,enumerable:false,configurable:true}})call、apply、bind实现://callFunction.prototype.myCall=函数(内容,...args){内容=内容||窗户;content.fn=这个;让result=content.fn(...args);删除内容.fn;返回结果;};//applyFunction.prototype.myApply=function(content,arr){content=content||窗户;content.fn=这个;让结果=arr&&content.fn(...arr);删除内容.fn;返回结果;};//bindFunction.prototype.myBind=function(content,...args){constthat=this;returnfunctionF(){if(thisinstanceofF){returnnewthat(...args,...arguments);//因为返回的是一个函数,我们可以newF(),所以需要判断}returnthat.apply(content,[...args,...arguments]);};};Currying://固定长度的currying函数curry(fn,...presetArgs){returnfunction(...args){letallargs=[...presetArgs,...args];如果(allargs.length>=fn.length){fn.apply(this,allargs);}else{returncurry(fn,...allargs);}};}//累加函数functionadd(){letargs=[...arguments];让加法器=函数(){args.push(...参数);返回加法器;}adder.toString=function(){returnargs.reduce((a,b)=>a+b)}returnadder;}new、instanceof、object.create实现://newfunctionmyNew(Con,...args){让对象={};obj.__proto__=Con.prototype;letresult=Con.call(obj,...args);返回结果实例对象?result:obj;}//instanceoffunctionmyInstanceof(left,right){让原型=right.prototype;左=左.__proto__;while(true){if(left===null||left===undefined)returnfalse;if(prototype===left)returntrue;左=左.__proto__;}}//object.createfunctionmyCreate(p){functionF(){}F.prototype=p;returnnewF();}其他://数组mapfunctionmap(arr,callback){if(Array.isArray(arr)){letnewArr=[];for(leti=0;i
