<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          15個(gè)web前端自定義函數(shù)工具庫

          共 6218字,需瀏覽 13分鐘

           ·

          2021-02-19 13:29

          來源 |?http://www.fly63.com/article/detial/10164



          1、call函數(shù)封裝實(shí)現(xiàn)

          // 手寫call函數(shù)function call(Fn,obj,...arg){    // 如果obj為null或者undefined,則指向window    if(obj === undefined || obj === null){        // globalThis是ES11的新特性,指向全局        obj = globalThis    }    //為obj添加臨時(shí)方法    obj.temp = Fn    // 調(diào)用 temp 方法    let result = obj.temp(...arg)    // 刪除obj 的 temp     delete obj.temp    return result}
          function add(a,b){ console.log(this); return a + b + this.c}let obj = { c:521}globalThis.c = 1314console.log(call(add,obj,10,20)); //551console.log(call(add,null,10,20)); //1344
          // 手寫call函數(shù)Function.prototype.call = function(obj,...arg){    // 如果obj為null或者undefined,則指向window    if(obj === undefined || obj === null){        // globalThis是ES11的新特性,指向全局        obj = globalThis    }    //為obj添加臨時(shí)方法    obj.temp = this    // 調(diào)用 temp 方法    let result = obj.temp(...arg)    // 刪除obj 的 temp     delete obj.temp    return result}function add(a,b){    console.log(this);    return a + b + this.c}let obj = {    c:521}globalThis.c = 1314console.log(add.call(obj,10,20)); //551console.log(add.call(null,10,20)); //1344

          2、apply函數(shù)封裝實(shí)現(xiàn)

          // 手寫apply函數(shù)Function.prototype.apply = function(obj,arg){    if(obj === null || obj === undefined){        obj = globalThis    }    obj.temp = this    let result = obj.temp(...arg)    delete obj.temp    return result}function add(a,b){    console.log(a+b+this.c);}let obj = {    c:1314}globalThis.c = 520add.apply(obj,[10,20]) //1344add.apply(null,[10,20]) //550

          3、bind函數(shù)封裝實(shí)現(xiàn)(bind不會(huì)立刻執(zhí)行)

          function bind(Fn,obj,...args){    if(obj === null || obj === undefined){        obj = globalThis    }    //bind返回一個(gè)函數(shù),調(diào)用的時(shí)候執(zhí)行方法    return function(...args2){        // 調(diào)用call方法        obj.temp = Fn        let result = obj.temp(...args,...args2)        delete obj.temp        return result    }}function add(a,b){    console.log(arguments);    console.log(a+b+this.c);}let obj = {    c:1314}globalThis.c = 520bind(add,obj,10,20)() //1344bind(add,null,10,20)(30,40) //550

          4、函數(shù)節(jié)流

          throttle節(jié)流
          語法:throttle(callback,wait)
          功能:創(chuàng)建一個(gè)節(jié)流函數(shù),在wait毫秒內(nèi)最多執(zhí)行callback一次
          理解:在函數(shù)需要頻繁觸發(fā)時(shí):函數(shù)執(zhí)行一次后,只有大于設(shè)定的執(zhí)行周期后才會(huì)執(zhí)行第二次,適合多次事件按時(shí)間做平均分配觸發(fā)
          場(chǎng)景:窗口調(diào)整(resize)、頁面滾動(dòng)(scroll)、DOM元素的拖拽功能實(shí)現(xiàn)(mousemove)、搶購瘋狂點(diǎn)擊(click)

          ?5、函數(shù)防抖

          語法:debounce(callback,wait)
          功能:創(chuàng)建一個(gè)防抖動(dòng)函數(shù),該函數(shù)會(huì)從上一次被調(diào)用后,延遲wait毫秒后調(diào)用callback
          理解:觸發(fā)了一次函數(shù)會(huì)在延遲wait毫秒后調(diào)用callback,但再次點(diǎn)擊,會(huì)清空掉再次計(jì)算
          場(chǎng)景:input框輸入時(shí)
                  

          6、數(shù)組函數(shù)map封裝實(shí)現(xiàn)

          map()方法創(chuàng)建一個(gè)新數(shù)組,其結(jié)果是該數(shù)組中的每個(gè)元素是調(diào)用一次提供的函數(shù)后的返回值
          const arr = [1,2,3,4,5]Array.prototype.map = function (callback) {    let result = []    for(let i = 0;i        result.push(callback(this[i],i))    }    return result}let arr2 = arr.map((item,index) => {    return item *10})console.log(arr2);

          7、數(shù)組函數(shù)reduce封裝實(shí)現(xiàn)

          reduce():從左到右為每個(gè)數(shù)組元素執(zhí)行一次回調(diào)函數(shù),并把上次回調(diào)函數(shù)的返回值放在一個(gè)暫存器中傳給下次回調(diào)函數(shù),并返回最后一次回調(diào)函數(shù)的返回值。
          const arr = [1,2,3,4,5]// 示例let result = arr.reduce((res,value)=>{    return res + value},0) //0為res初始值,value為arr的值console.log(result); //15
          Array.prototype.reduce = function(callback,value){ let result = value for(let i = 0;i result = callback(result,this[i]) } return result}// 演示let arr2 = arr.reduce((res,value)=>{ return res + value},5)console.log(arr2);
          8、數(shù)組函數(shù)filter封裝實(shí)現(xiàn)
          filter():將所有在過濾函數(shù)中返回true的數(shù)組元素放進(jìn)一個(gè)新數(shù)組中并且返回
          const arr = [1,2,3,4,5]Array.prototype.filter2 = function(callback){    let arr = []    for(let i = 0;i        if(callback(this[i],i)){            arr.push(this[i])        }    }    return arr}let res = arr.filter2((item=>{    return item > 2}))console.log(res);

          8、數(shù)組函數(shù)find封裝實(shí)現(xiàn)

          find():找到第一個(gè)滿足測(cè)試函數(shù)的元素并返回那個(gè)元素的值,如果找不到,則返回undefined

          const arr = [1,2,3,2005,4,1001]// find()Array.prototype.find = function(callback){    for(let i = 0;i        if(callback(this[i],i)){            return this[i]        }    }    return undefined}let res = arr.find((item=>{    return item > 3000}))console.log(res);

          9、數(shù)組函數(shù)findIndex封裝實(shí)現(xiàn)

          findIndex():找到第一個(gè)滿足測(cè)試函數(shù)的元素并返回那個(gè)元素的索引,如果找不到,則返回-1。
          // findIndex()Array.prototype.findIndex2 = function(callback){    for(let i = 0;i        if(callback(this[i],i)){            return i        }    }    return -1}let res = arr.findIndex2((item=>{    return item > 1000}))console.log(res);

          10、數(shù)組函數(shù)every封裝實(shí)現(xiàn)

          every():如果數(shù)組中的每個(gè)元素都滿足測(cè)試函數(shù),則返回true,否則返回false
          const arr = [1,2,3,4,5]Array.prototype.every2 = function(callback){    for(let i = 0;i        let result = callback(this[i],i)        if(!result){            return false;        }    }    return true}const result = arr.every2(item=>{    return item > 0})console.log(result);

          11、數(shù)組函數(shù)some封裝實(shí)現(xiàn)

          some():如果數(shù)組中至少有一個(gè)元素滿足測(cè)試函數(shù),則返回true,否則返回false
          Array.prototype.some2 = function(callback){    for(let i = 0;i        let result = callback(this[i],i)        if(result){            return true        }    }    return false;}const result = arr.some2(item=>{    return item > 6})console.log(result);

          12、數(shù)組去重

          方法1:利用foreach()和indexOf()
          方法2:利用froecah() + 對(duì)象容器
          方法3:利用ES6語法:from + Set 或者 ... + Set
          const arr = [1,2,3,4,5,2,4]// 方法1 :forEach + indexOf function unique(arr){    if(!Array.isArray(arr)){        return    }    let result = []    arr.forEach(item=>{        if(result.indexOf(item) === -1){            result.push(item)        }    })    return result}let result = unique(arr)console.log(result);// 方法2 forEach() + 對(duì)象容器function unique2(arr){    let result = []    //聲明空對(duì)象    const obj = {}    arr.forEach(item => {        if(obj[item] === undefined){            obj[item] = true            result.push(item)        }    })    console.log(obj);    return result
          }let result2 = unique2(arr)console.log(result2);//方法3:利用ES6語法:from + Set 或者 ... + Setfunction unique3(arr){ return [...new Set(arr)] // let result = Array.from(new Set(arr)) // return result}let result3 = unique3(arr)console.log(result2);

          13、數(shù)組合并和切片

          數(shù)組合并concat()
          let arr = [1,2,3]Array.prototype.concat2 = function(...args){    let result = [...this,...args]    return result}const result = arr.concat2([4,5,6],7,8)console.log(result);

          數(shù)組切片slice()

          Array.prototype.slice2 = function(begin,end){    if(this.length === 0){        return [];    }    //判斷begin    begin = begin || 0    if(begin >= this.length){        return [];    }    //判斷end    end = end || this.length    if(end         end = this.length    }    let result = []    this.forEach((item,index)=>{        if(index >= begin && index < end){            result.push(item)        }    })    return result}let sliceResult = arr.slice2(1,6)console.log(sliceResult);

          14、數(shù)組扁平化

          語法:flatten(array)
          取出嵌套數(shù)組(多維)中的所有元素放到一個(gè)新數(shù)組(一維)中
          例如:[ 1,[ 3,[ 2,4 ]]]? -> [1,2,3,4]
          方法1:遞歸 + reduce() + concat()
          方法2:,,, + some()? + concat()?
          let arr = [1,2,[3,4,[5,6]],7]// 方法1function falttenl(arr){    let result = []    arr.forEach(item => {        if(Array.isArray(item)){            result = result.concat(falttenl(item))        }else{            result = result.concat(item)        }    });    return result}console.log(falttenl(arr));// 方法2function flatten2(arr){    let result = [...arr]    while(result.some(item => Array.isArray(item))){        result = [].concat(...result)    }    return result}console.log(flatten2(arr));

          15、數(shù)組分塊

          語法:chunk(array,size)
          功能:將數(shù)組拆分成多個(gè)size長(zhǎng)度的區(qū)塊,每個(gè)區(qū)塊組成小數(shù)組,整體組成一個(gè)二維數(shù)組
          如:[1,2,3,4,5,6]調(diào)用chunk(arr,4) ===> [[1,2,3,4],[5,6]]

          本文完?

          瀏覽 50
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人网中文字幕 | 九色麻豆 | 欧美熟妇乱| 东京热视频专区 | 日本尤物在线免费观看 |