楂樻ゼ鎷斿湴鑰岃捣锛屽湴鍩虹墷鍥猴紝鏂硅兘绔嬩簬涓嶈触涔嬪湴銆備粖澶╃粰澶у甯︽潵鐨勬槸10涓父鐢ㄧ殑JavaScript鎵嬪啓鍑芥暟锛岄噸瑕佺殑鍦版柟閮藉姞浜嗘敞閲娿€傛湁浜涙槸浠庡埆浜洪偅閲屽€熸潵鐨勶紝鏈変簺鏄嚜宸卞啓鐨勩€傚鏋滄湁涓嶅噯纭殑鍦版柟锛岃鎸囨銆?.闃叉姈鍔熻兘debounce(fn,delay){lettimerreturnfunction(...args){if(timer){clearTimeout(timer)}timer=setTimeout(()=>{fn.apply(this,args)},delay)}}//娴嬭瘯鍑芥暟task(){console.log('runtask')}constdebounceTask=debounce(task,1000)window.addEventListener('scroll',debounceTask)2.鑺傛祦鍑芥暟throttle(fn,delay){letlast=0//鏈€鍚庤Е鍙戞椂闂磖eturn(...args)=>{constnow=Date.now()if(now-last>delay){last=nowfn.apply(this,args)}}}//娴嬭瘯鍑芥暟task(){console.log('runtask')}constthrottleTask=throttle(task,1000)window.addEventListener('scroll',throttleTask)3.娣辨嫹璐濆嚱鏁癲eepClone(obj,cache=newWeakMap()){if(typeofobj!=='object')returnobj//鏅€氱被鍨嬶紝鐩存帴杩斿洖if(obj===null)returnobjif(cache.get(obj))returncache.get(obj)//闃叉寰幆寮曠敤锛岀▼搴忚繘鍏ユ寰幆if(objinstanceofDate)returnnewDate(obj)if(objinstanceofRegExp)returnnewR渚嬪Exp(obj)//鎵惧埌鍘熷瀷涓婄殑鏋勯€犲嚱鏁帮紝鍘熷瀷涓婄殑鏋勯€犲嚱鏁版寚鍚戝綋鍓嶅璞$殑鏋勯€犲嚱鏁發etcloneObj=newobj.constructor()cache.set(obj,cloneObj)//缂撳瓨澶嶅埗鐨勫璞″鐞嗗惊鐜紩鐢╟asefor(letkeyinobj){if(obj.hasOwnProperty(key)){cloneObj[key]=deepClone(obj[key],cache)//閫掑綊澶嶅埗}}returncloneObj}//testconstobj={name:'Jack',address:{x:100,y:200}}obj.a=obj//寰幆寮曠敤constnewObj=deepClone(obj)console.log(newObj.address===obj.address)//false4銆両mplementPromiseclassMyPromise{constructor(executor){//鎵ц鍣╡xecutorthis.status='pending'//绛夊緟鐘舵€乼his.value=null//鎴愬姛鎴栧け璐ュ弬鏁皌his.fulfilledCallbacks=[]//鎴愬姛鍑芥暟闃熷垪this.rejectedCallbacks=[]//澶辫触鍑芥暟闃熷垪constthat=thisfunctionresolve(value){//鎴愬姛鏂规硶if(that.status==='pending'){that.status='resolved'that.value=valuethat.fulfilledCallbacks.forEach(myFn=>myFn(that.value))//鎵ц鍥炶皟鏂规硶}}functionreject(value){//鏂规硶澶辫触if(that.status==='pending'){that.status='rejected'that.value=valuethat.rejectedCallbacks.forEach(myFn=>myFn(that.value))//鎵ц鍥炶皟鏂规硶}}try{executor(resolve,reject)}catch(err){reject(err)}}then(onFulfilled,onRejected){if(this.status==='pending'){//绛夊緟鐘舵€侊紝灏嗗洖璋冨嚱鏁板姞鍏ユ垚鍔熷嚱鏁伴槦鍒梩his.fulfilledCallbacks.push(()=>{onFulfilled(this.value)})//绛夊緟鐘舵€侊紝灏嗗洖璋冨嚱鏁板姞鍏ュけ璐ュ嚱鏁伴槦鍒梩his.rejectedCallbacks.push(()=>{onRejected(this.value)})}if(this.status==='resolved'){//鏀寔鍚屾璋冪敤console.log('this',this)onFulfilled(this.value)}if(this.status==='rejected'){//鏀寔璋冪敤onRejected(this.value)鍚屾}}}//娴嬭瘯鍑芥暟fn(){returnnewMyPromise((resolve,reject)=>{setTimeout(()=>{if(Math.random()>0.6){resolve(1)}else{reject(2)}},1000)})}fn().then(res=>{console.log('res',res)//res1},err=>{console.log('err',err)//err2})5.寮傛鎺у埗骞跺彂鍑芥暟limitRequest(urls=[],limit=3){returnnewPromise((resolve,reject)=>{constlen=urls.lengthletcount=0//褰撳墠浠诲姟鏁癱onststart=async()=>{consturl=urls.shift()//浠庢暟缁勪腑鑾峰彇绗竴涓换鍔f(url){try{awaitaxios.post(url)if(count==len-1){//鏈€鍚庝竴涓换鍔℃垚鍔焤esolve()}else{count++//鎴愬姛锛屽紑濮嬩笅涓€涓换鍔tart()}}catch(e){if(count==len-1){//鏈€鍚庝竴涓换鍔″け璐esolve()}else{count++//澶辫触锛屽紑濮嬩笅涓€涓换鍔tart()}}}}//寮€濮嬮檺鍒朵换鍔hile(limit>0){start()limit-=1}})}//娴嬭瘯limitRequest(['http://xxa','http://xxb','http://xxc','http://xxd','http://xxe'])6銆丒S5缁ф壙锛堝瘎鐢熺粍鍚堢户鎵縤nherit)functionParent(name){this.name=name}Parent.prototype.eat=function(){console.log(this.name+'iseating')}functionChild(name,age){Parent.call(this,name)this.age=age}Child.prototype=Object.create(Parent.prototype)Child.prototype.contructor=ChildChild.prototype.study=function(){console.log(this.name+'姝e湪瀛︿範')}//testletchild=newChild('xiaoming',16)console.log(child.name)//xiaomingchild.eat()//xiaomingiseatingchild.study()//xiaomingisstudying7,鏁扮粍鎺掑簭Sorting//瀵规暟瀛楄繘琛屾帓搴忥紝绠€鍐檆onstarr=[3,2,4,1,5]arr.sort((a,b)=>a-b)console.log(arr)//[1,2,3,4,5]//瀛楁瘝鎺掑簭锛岀畝鍐檆onstarr=['b','c','a','e','d']arr.sort()console.log(arr)//['a','b','c','d','e']鍐掓场鎺掑簭鍑芥暟bubbleSort(arr){letlen=arr.lengthfor(leti=0;i
