Skip to content

实现 Function.prototype.call

Posted on:2021年7月7日 at 00:13
Function.prototype.call = function (context = window, ...args) {
  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;
};
原文转自:https://fe.ecool.fun/topic/827a8f47-1144-4ab1-9d6c-b7d3b3d6aebf