火影推荐程序连载31-bind返回的是一个函数体

bind返回的是一个函数体

call和apply会直接执行,但是call参数需要一个一个进行传递,apply的第二个参数是一个数组

实现

bind

简单实现

复制代码

Function.prototype.myBind = function(context){
  self = this;  //保存this,即调用bind方法的目标函数
  return function(){
      return self.applay(context, [...arguments]);
  };
};

复制代码

考虑到函数柯里化的实现

复制代码

Function.prototype.myBind = function(context){
  // 使用闭包存下初始参数
  var args = Array.prototype.slice.call(arguments, 1),
  self = this;
  return function() {
        // 再次调用时
      var innerArgs = Array.prototype.slice.call(arguments);
      var finalArgs = args.concat(innerArgs);
      return self.apply(context,finalArgs);
  };
};

复制代码

考虑构造函数的实现

复制代码

Function.prototype.myBind = function(context){
  // 判断是否为函数
  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;
};

复制代码

call

思路

复制代码

// 要实现这个效果
var foo ={
  value:1
}
function bar(){
  console.log(this.value)
}
bar.call(foo);//1

// 相当于做如下事情
var foo = {
    value: 1,
    bar: function() {
        console.log(this.value)
    }
};
foo.bar(); // 1

复制代码

​实现

复制代码

Function.Prototype.myCall = function(context) {
  // this 参数可以传 null,当为 null 的时候,视为指向 window
  var context = context || window;
    context.fn = this;
  // 对参数进行处理
    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;
  * 1. The www.yachengyl.cn protocol www.fudayulpt.cn configured www.ued3zc.cn the client is inconsistent with the protocol of the server.
  
  * eg: consumer www.baichuangyule.cn www. jinmazx.cn www.bhylzc.cn protocol www.jintianxuesha.com= dubbo, provider only has other protocol services(rest).
  
  * 2. The registration www.xinhuihpw.com center www.wanyayuue.cn not robust and pushes illegal specification data.
  
  if (CollectionUtils.www.feihongyul.cn isEmptyMap(newUrlInvokerMap)) www.qiaoheibpt.com{
  
  logger.error(new www.shengrenpt.com IllegalStateException("urls to invokers error .invokerUrls.size :" + invokerUrls.size() + ", invoker.size www.baishenjzc.cn :0.
  
  List<Invoker<www.huanhua2zhuc.cn>> newInvokers = Collections.unmodifiableList(new ArrayList<>(newUrlInvokerMap.values(www.baihuayl7.cn)));
  
  // pre-route and build cache, notice that route cache should build on www.shengrenyp.cn original Invoker list.
  
  // toMergeMethodInvokerMap(www.xinchenptgw.cn ) will wrap some invokers www.xinhuihpw.com having www.youxiu2pt.cn different groups, those wrapped invokers not should be routed.
  
  routerChain.setInvokers(www.haojuptzc.cn newInvokers);
  
  this.invokers =www.wujiu5zhuce.cn
Function.Prototype.myApply = function(context, arr) {
  // this 参数可以传 null,当为 null 的时候,视为指向 window
  var context = context || window;
    context.fn = this;
  let result = arr.length>0 ? context.fn(...arr) : context.fn();
  delete context.fn;
  return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值