手写bind

手写bind

Function.prototype.bind2 = function (context) {
  context = context || globalThis;
  const argsTemp = [...arguments];
  argsTemp.shift();
  const _symbol = Symbol();
  context[_symbol] = this;
  return function (...args) {
    let result = context[_symbol](...argsTemp, ...args);
    delete context[_symbol];
    return result;
  }
}

参考文献

[1]高阶组件手写实现篇