实现call apply bind改变this指向

call

复杂的实现方式

  Function.prototype._call = function (context) {
    context = context || window;
    context.fn = this;

    var args = [];
    for(var i = 1, len = arguments.length; i < len; i++) {
      // 传递字符串参数时,在这里会先解析字符串参数,
      // 然后在 eval 中," " + args + " " 这样的字符串拼接,会让原来的字符串参数变成普通参数
      // 所以 eval执行时,就不是字符串参数了,会报错
      // 因此把 arguments 解析的过程放在eval中
        args.push('arguments[' + i + ']');
    }
    // 数组转换成字符串,会变成 1,2,3形式
    // context.fn(arguments[1], arguments[2], auguments[3])
    eval('context.fn(' + args +')');

    delete context.fn
}

es6的实现方式,用剩余操作符接收 没有对应形参的参数,剩余操作符和arguments的不同:

  • 剩余参数只包含没有对应形参的实参
  • 剩余数组是真的数组,不是类数组,所以可以使用数组方法
  • arguments对象有其他附加的属性(比如callee属性)
Function.prototype.call = function (context, ...restArgs) {
  var context = context || window;
  context.fn = this;
  // console.log(restArgs)    输出 1,2,3
  // 一定要记住!!!eval相当于直接执行
  // 不要再用字符串拼接,直接调用!!!否则还是会字符串先解析一次
  var result = eval('context.fn(...restArgs)');

  delete context.fn
  return result;
}

apply 实现

// arr 接受传入的参数数组
Function.prototype.apply_ = function (obj, arr) {
    obj = obj ? Object(obj) : window;
    obj.fn = this;
    // 没有参数数组,直接执行
    if (!arr) {
        obj.fn();
    // 有参数,用数组存储
    } else {
        var args = [];
        // 注意这里的i从0开始
        for (var i = 0, len = arr.length; i < len; i++) {
            args.push("arr[" + i + "]");
        };
        eval("obj.fn(" + args + ")"); // 执行fn
    };
    delete obj.fn; //删除fn
};
fn.apply_(obj, ["我的", "名字", "是"]); // 我的名字是听风是风
fn.apply_(null, ["我的", "名字", "是"]); // 我的名字是时间跳跃
fn.apply_(undefined, ["我的", "名字", "是"]); // 我的名字是时间跳跃

es6实现,注意与call的不同

Function.prototype.call = function (context, args) {
  var context = context || window;
  context.fn = this;

  var result = eval('context.fn(...args)');

  delete context.fn
  return result;
}

bind 实现

bind改变this指向是隐式调用 call 方法,注意 构造函数 也可以用bind重新绑定this

Function.prototype._bind = function(obj, ...args) {
  // 异常处理,判断要改变指向的是不是函数
  if(typeof this != 'function') throw new Error('what is trying to be bound is not callable')
  // 保存调用的函数
  var fn = this

  let fBound = function() {
    // new 的时候隐式创建 this,指向实例
    // 判断 this 是否是由new创建的,是的话就让函数的this变为实例的this
    return fn.apply(this instanceof fBound ? this : obj, args.concat(Array.prototype.slice.call(arguments)))
  }
  // 用空对象当作 fBound构造函数和实例 继承 fn 的桥梁
  let fNOP = function() {}
  if(fn.prototype) {
    fNOP.prototype = fn.prototype
  }
  // 这样防止修改 fBound 原型的时候,也改变 fn 的原型
  fBound.prototype = new fNOP()
  return fBound
}

// 调用
function run(a, b) {
  this.val = '1'
  console.log(this.name + a + 'running'+b)
}

let obj = {
  name: 'kathy'
}
let Irun = run._bind(obj, 'a')

let instance = new Irun('b')
console.log(instance.val)

最后把 new fn() 的效果和 new fBound 的效果是一致的

参考:前端面试题——自己实现bind

results matching ""

    No results matching ""