es6 手动实现call 实现原理

127 阅读1分钟
  Function.prototype.es6Call = function (context, ...arg) {
    let con = context || window;

    con.fn = this;

    let res = con.fn(...arg);

    delete res;

    return res;
  };
  let a = {
    name: 'jack',
    getName: function (msg) {
      return msg + this.name;
    },
  };
  let b = {
    name: 'lily',
  };

  console.log(a.getName.es6Call(b, 'hi~')); // hi~lily