九九色综合_性69交片免费看_亚洲一区二区三区久久精品_精品一久久_欧美一级黄视频_污免费网站

call、apply、bind 原理實現

2020-4-6    seo達人

目錄

  1. call 的模擬實現
  2. apply 的模擬實現
  3. bind 的模擬實現
  4. 三者異同



    學習并參考于:



    JavaScript深入之call和apply的模擬實現



    JS的call,apply與bind詳解,及其模擬實現





    (一)call的模擬實現

    call 用法 : MDN Function.prototype.call()



    call() 方法使用一個指定的 this 值和可選的參數列表來調用一個函數。



    call() 提供新的 this 值給當前調用的函數/方法。


  5. call 實現主要思路:

    將函數設為對象的屬性



    執行該函數



    刪除該函數



    另外還有考慮:



    call 函數還能給定參數執行函數

    this 參數不傳,或者傳null,undefined, this指向window對象

    函數是可以有返回值的
  6. 實現:

    Function.prototype.myCall = function () {

      if (typeof this !== 'function') {

        throw new TypeError('error!')

      }

      let context = arguments[0] || window   //this 參數可以傳 null,當為 null 的時候,視為指向 window

      context.fn = this  // 首先要獲取調用call的函數,用this可以獲取

      let args = [...arguments].slice(1) //從 Arguments 對象中取值,取出第二個到最后一個參數   

      let result = context.fn(...args)  //函數是可以有返回值的

      delete context.fn

      return result

    }


  7. 測試:

    // 測試一下上面實現的myCall

    var value = 2;



    var obj = {

        value: 1

    }



    function bar(name, age) {

        console.log(this.value);

        return {

            value: this.value,

            name: name,

            age: age

        }

    }



    bar.call(null); // 2



    console.log(bar.myCall(obj, 'kevin', 18));

    // 1

    // Object {

    //    value: 1,

    //    name: 'kevin',

    //    age: 18

    // }



    (二)apply 的模擬實現

    apply 用法:MDN Function.prototype.apply()



    apply() 方法使用一個指定的 this 值和可選的參數數組 來調用一個函數。



    apply 的實現跟 call 類似。


  8. 實現:

    Function.prototype.myApply = function () {

      if (typeof this !== 'function') {

        throw new TypeError('error!')

      }

      let context = arguments[0] || window

      context.fn = this

      let result = arguments[1] ? context.fn(...arguments[1]) : context.fn()

      delete context.fn

      return result

    }


  9. 測試:

    var foo = {

        value: 1

    }

    function bar(name, age) {

        console.log(name)

        console.log(age)

        console.log(this.value);

    }

    bar.myApply(foo, ['black', '18']) // black 18 1



    (三)bind 的模擬實現

    bind 用法:MDN Function.prototype.bind()



    bind()方法會創建一個新函數,稱為綁定函數。當這個新函數被調用時,bind() 的第一個參數將作為它運行時的 this,之后的一序列參數將會在傳遞的實參前傳入作為它的參數。



    bind是ES5新增的一個方法,不會執行對應的函數,而是返回對綁定函數的引用。


  10. 實現:

    Function.prototype.customBind = function () {

      if (typeof this !== 'function') {

        throw new TypeError('error!')

      }

      const that = this   // 首先要獲取調用bind的函數,用this獲取并存放在that中

      let context = arguments[0] || window

      const args = [...arguments].slice(1)

      return function() {

        return that.apply(context, args.concat([...arguments]))

      }

    }



    (四)三者異同
  11. 相同:

    改變函數體內 this 的指向
  12. 不同:

    call、apply的區別:call方法接受的是參數列表,而apply方法接受的是一個參數數組。

    bind不立即執行。而call或apply會自動執行對應的函數。


日歷

鏈接

個人資料

藍藍設計的小編 http://m.gyxygd.cn

存檔

主站蜘蛛池模板: 欧美日韩视频在线第一区 | 日本精品免费观看 | 成人国产精品高清在线观看 | 色黄视频在线 | 欧美成人午夜剧场 | 爽爽爽爽爽爽a成人免费视频 | 久久综合九色欧美综合狠狠 | 午夜影院官网 | 亚洲国产成人久久综合野外 | 久草精品视频 | 国产精品成人av | 久久国产亚洲欧美日韩精品 | 亚洲一区二区精品视频 | 色花堂的网站地址 | 色网址在线观看 | 亚洲一区在线日韩在线深爱 | 日本中文字幕一区 | 久青草免费视频手机在线观看 | 一区二区三区国 | 日本一级免费 | 国产一二三 | 亚洲欧美一区二区三区不卡 | 自拍偷拍精品 | 日韩欧美中文 | 国产精品视频久久久久久 | 亚洲精品乱码蜜桃久久久 | 日韩精品免费视频 | 娇喘在线| 狠狠色狠狠色很很综合很久久 | 亚洲视频精品在线观看 | 久久综合九色综合欧美狠狠 | 亚洲美女毛片 | www.日韩系列 | 在线观看91 | 你懂的国产精品 | 欧美毛片性视频区 | 大色综合色综合网站 | 一个人看的www片高清小说 | 国产免费久久精品国产传媒 | 国产欧美一区二区三区观看 | 狠狠躁天天躁夜夜躁婷婷 |