Skip to content

call && apply ​

js
Function.prototype.call = (context = window) => {
  context.fn = this
  const args = [...arguments].slice(1)
  const res = context.fn(...args)
  delete context.fn
  return res
}
js
Function.prototype.apply = (context = window, args) => {
  context.fn = this;
  let res;

  if (args.length) {
    res = context.fn(args);
  } else {
    res = context.fn();
  }

  delete context.fn;
  return res;
};
前往 GitHub 查看或发表评论