乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      bind,call,apply模擬實現(xiàn)

       昵稱71567378 2020-09-17

      首先,三者第一個參數(shù)都為this指向

      區(qū)別

      bind返回的是一個函數(shù)體

      call和apply會直接執(zhí)行,但是call參數(shù)需要一個一個進(jìn)行傳遞,apply的第二個參數(shù)是一個數(shù)組

      實現(xiàn)

      bind

      簡單實現(xiàn)

      復(fù)制代碼

      Function.prototype.myBind = function(context){
        self = this;  //保存this,即調(diào)用bind方法的目標(biāo)函數(shù)
        return function(){      return self.applay(context, [...arguments]);
        };
      };

      復(fù)制代碼

      考慮到函數(shù)柯里化的實現(xiàn)

      復(fù)制代碼

      Function.prototype.myBind = function(context){  // 使用閉包存下初始參數(shù)
        var args = Array.prototype.slice.call(arguments, 1),
        self = this;  return function() {        // 再次調(diào)用時
            var innerArgs = Array.prototype.slice.call(arguments);      var finalArgs = args.concat(innerArgs);      return self.apply(context,finalArgs);
        };
      };

      復(fù)制代碼

      考慮構(gòu)造函數(shù)的實現(xiàn)

      復(fù)制代碼

      Function.prototype.myBind = function(context){  // 判斷是否為函數(shù)
        if(typeof this !== 'function') {    throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
        }  var args = Array.prototype.slice(arguments, 1);
        self = this;
        bound = function() {      var innerArgs = Array.prototype.slice.call(arguments);      var finalArgs = args.concat(innerArgs);      // 判斷this,如果是,則傳遞this即實例化的對象。
            return self.apply((this instanceof F ? this : context), finalArgs);
        };  // 處理原型鏈
        let F = function(){};
        F.prototype = self.prototype;
        bound.prototype = new F();
      
        retrun bound;
      };

      復(fù)制代碼

      call

      思路

      復(fù)制代碼

      // 要實現(xiàn)這個效果var foo ={
        value:1}function bar(){
        console.log(this.value)
      }
      bar.call(foo);//1// 相當(dāng)于做如下事情var foo = {
          value: 1,
          bar: function() {
              console.log(this.value)
          }
      };
      foo.bar(); // 1

      復(fù)制代碼

      實現(xiàn)

      復(fù)制代碼

      Function.Prototype.myCall = function(context) {  // this 參數(shù)可以傳 null,當(dāng)為 null 的時候,視為指向 window
        var context = context || window;
          context.fn = this;  // 對參數(shù)進(jìn)行處理
          var args = [];  for(var i = 1, len = arguments.length; i < len; i++) {
            args.push(arguments[i]);
        }
        let result = arguments.length>0 ? context.fn(...args) : context.fn();  delete context.fn;  return result;
      }

      復(fù)制代碼

      apply

      復(fù)制代碼

      Function.Prototype.myApply = function(context, arr) {  // this 參數(shù)可以傳 null,當(dāng)為 null 的時候,視為指向 window
        var context = context || window;
          context.fn = this;
        let result = arr.length>0 ? context.fn(...arr) : context.fn();  delete context.fn;  return result;
      }

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多