call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。

需要注意的点有,

  1. 挂载到 Function 的原型上
  2. 具有 thisArgs 和 原函数所需的其他参数
  3. 需要判断 thisArgs 是否为 undefined, 如果为 undefined 要他赋值全局的对象。
  4. 返回 函数调用结果
js
Function.prototype.myCall = function (thisArg, ...args) { if (thisArg) { thisArg = Object(thisArg); } else { thisArg = typeof window !== "undefined" ? window : global; } thisArg._fn = this; const result = thisArg._fn(...args); delete thisArg._fn; return result; };
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:叶继伟

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!