Underscore源碼閱讀:bind

bind函數(shù)

參考:JavaScript深入之bind的模擬實(shí)現(xiàn)

bind(function, object, *arguments):綁定函數(shù) function 到對(duì)象 object 上, 也就是無論何時(shí)調(diào)用函數(shù), 函數(shù)里的 this 都指向這個(gè) object。

我們先看上面的參考博文的實(shí)現(xiàn),其中有幾個(gè)要點(diǎn):

  1. bind的基本實(shí)現(xiàn)(用閉包保存bind傳進(jìn)來的上下文)
  2. bind是可以傳部分或者全部參數(shù)的
  3. bind后的函數(shù)是依舊可以當(dāng)做構(gòu)造函數(shù)來使用并且忽略bind提供的this

再結(jié)合《你不知道的JavaScript(上)》里的bind,我們可以得到這樣一個(gè)bind

Function.prototype.myBind = function (context) {
  context = Object(context) || window;
  var self = this;
  var args = [].slice.call(arguments, 1);

  var constructor = function () {
    args = args.concat([].slice.call(arguments));
    if (this instanceof self) {
      // 說明正在使用new
      return self.apply(this, args);
    } else {
      // 普通的bind使用方法
      return self.apply(context, args);
   }
  }
  constructor.prototype = Object.create(self.prototype);
  return constructor;
}

然后我們來看看underscore中的_.bind

  _.bind = function (func, context) {
    // nativeBind
    if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
    // 上下文校驗(yàn)
    if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
    // 保存部分參數(shù)
    var args = slice.call(arguments, 2);
    var bound = function () {
      return executeBound(func, bound, context, this, args.concat(slice.call(arguments)));
    };
    return bound;
  };

我們也看到bind函數(shù)實(shí)現(xiàn)的核心是executeBound函數(shù),它有5個(gè)參數(shù)

  • funcbind的目標(biāo)函數(shù)
  • boundbind的返回函數(shù)
  • context:綁定上下文
  • this:執(zhí)行上下文
  • args:執(zhí)行參數(shù)
    明白了參數(shù)的意義,我們?cè)賮砜?code>executeBound的實(shí)現(xiàn)
  var executeBound = function (sourceFunc, boundFunc, context, callingContext, args) {
    if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
    var self = baseCreate(sourceFunc.prototype);
    var result = sourceFunc.apply(self, args);
    if (_.isObject(result)) return result;
    return self;
  };

其實(shí)思路就跟之前的bind函數(shù)是一樣的了。

  1. 判斷執(zhí)行上下文是否是new賦值的新this
  2. 如果不是,當(dāng)做普通的bind返回函數(shù)
  3. 如果是,則需要當(dāng)做構(gòu)造函數(shù)使用,就需要添加prototype和檢驗(yàn)返回結(jié)果
  4. baseCreate添加原型,result檢驗(yàn)構(gòu)造函數(shù)是否有返回值;
  5. 如果這個(gè)返回值是對(duì)象,就返回這個(gè)對(duì)象;否則返回構(gòu)造函數(shù)執(zhí)行的結(jié)果。

于是發(fā)現(xiàn),Underscore源碼實(shí)現(xiàn)的bind函數(shù),跟我們之前實(shí)現(xiàn)的是一樣的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 函數(shù)和對(duì)象 1、函數(shù) 1.1 函數(shù)概述 函數(shù)對(duì)于任何一門語言來說都是核心的概念。通過函數(shù)可以封裝任意多條語句,而且...
    道無虛閱讀 4,941評(píng)論 0 5
  • 第3章 基本概念 3.1 語法 3.2 關(guān)鍵字和保留字 3.3 變量 3.4 數(shù)據(jù)類型 5種簡單數(shù)據(jù)類型:Unde...
    RickCole閱讀 5,502評(píng)論 0 21
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,527評(píng)論 19 139
  • 人類的基因演化仍然一如既往慢如蝸牛,但人類的想象力卻是極速奔馳,建立起了地球上前所未有的大型合作網(wǎng)絡(luò)。——《人類簡...
    adamkings閱讀 204評(píng)論 0 1
  • onAttach()為碎片建立關(guān)聯(lián)的時(shí)候調(diào)用 onCreateView()為碎片創(chuàng)建視圖(加載布局)時(shí)調(diào)用 onA...
    plus彭于晏閱讀 461評(píng)論 0 0

友情鏈接更多精彩內(nèi)容