call()
方法使用一个指定的 this
值和单独给出的一个或多个参数来调用一个函数。
需要注意的点有,
Function
的原型上thisArgs
和 原函数所需的其他参数thisArgs
是否为 undefined
, 如果为 undefined
要他赋值全局的对象。jsFunction.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;
};
本文作者:叶继伟
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!