楂樻ゼ鎷斿湴鑰岃捣锛屽湴鍩虹墷鍥猴紝鏂硅兘绔嬩簬涓嶈触涔嬪湴銆備粖澶╃粰澶у甯︽潵鐨勬槸10涓父鐢ㄧ殑JavaScript鎵嬪啓鍑芥暟锛岄噸瑕佺殑鍦版柟閮藉姞浜嗘敞閲娿€傛湁浜涙槸浠庡埆浜洪偅閲屽€熸潵鐨勶紝鏈変簺鏄嚜宸卞啓鐨勩€傚鏋滄湁涓嶅噯纭殑鍦版柟锛岃鎸囨銆?.闃叉姈鍔熻兘debounce(fn,delay){璁╁畾鏃跺櫒杩斿洖鍑芥暟(...args){if(timer){clearTimeout(timer)}timer=setTimeout(()=>{fn.apply(this,args)},delay)}}//娴嬭瘯鍑芥暟task(){console.log('runtask')}constdebounceTask=debounce(task,1000)window.addEventListener('scroll',debounceTask)澶嶅埗浠g爜2锛岃妭娴佸嚱鏁皌hrottle(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)copy浠g爜3,deepCopyfunctiondeepClone(obj,cache=newWeakMap()){if(typeofobj!=='object')returnobj//鏅€氱被鍨嬶紝鐩存帴杩斿洖if(obj===null)returnobjif(cache.get(obj))returncache.get(obj)//闃叉寰幆寮曠敤锛岀▼搴忚繘鍏ユ寰幆if(objinstanceofDate)returnnewDate(obj)if(objinstanceofRegExp)returnnewRegExp(obj)//鎵惧埌鍘熷瀷鏋勯€犲嚱鏁帮紝鍘熷瀷涓婄殑鏋勯€犲嚱鏁版寚鍚戝綋鍓嶅墠涓€涓璞$殑鏋勯€犲嚱鏁發etcloneObj=newobj.constructor()cache.set(obj,cloneObj)//缂撳瓨澶嶅埗鐨勫璞℃潵澶勭悊寰幆寮曠敤for(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)//falsecopycode4,implementPromiseclassMyPromise{constructor(executor){//executorthis.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))//鎵ц鍥炶皟鏂规硶}}鍑芥暟nreject(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)})//绛夊緟鐘舵€侊紝鍚戞垚鍔熷嚱鏁伴槦鍒楁坊鍔犲洖璋冨嚱鏁癴ailedfunctionQueuethis.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})澶嶅埗浠g爜5銆佸紓姝ユ帶鍒跺苟鍙戝嚱鏁發imitRequest(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'])澶嶅埗浠g爜6銆丒S5缁ф壙锛堝瘎鐢熺粍鍚堢户鎵匡級鍑芥暟Parent(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+'isstudying')}//娴嬭瘯璁╁瀛?newChild('灏忔晱g',16)console.log(child.name)//xiaomingchild.eat()//xiaomingiseatingchild.study()//xiaomingiseatingchild.study()//xiaomingiseatingchild.study()//xiaomingiseatingchild.study()//xiaomingiseatingchild.study()//xiaomingsortsortsort//瀵规暟瀛楄繘琛屾帓搴忥紝缂╁啓constarr=[3,2,4,1,5]arr.sort((a,b)=>a-b)console.log(arr)//[1,2,3,4,5]//鎺掑簭瀛楁瘝锛岄€熻constarr=['b','c','a','e','d']arr.sort()console.log(arr)//['a','b','c','d','e']澶嶅埗浠g爜鍐掓场鎺掑簭鍑芥暟bubbleSort(arr){letlen=arr.lengthfor(leti=0;i
