Skip to content

实现 Function.prototype.apply()

第一个参数是绑定的this,默认为window,第二个参数是数组或类数组

js
Function.prototype.apply = function(context, args) {
   context = (context === undefined || context === null) ? globalThis : Object(context);
  if (typeof this !== 'function') {
    throw new TypeError('Type Error');
  }
  const fn = Symbol('fn');
  context[fn] = this;

  const res = context[fn](...args);
  delete context[fn];
  return res;
}