apply()
方法调用一个具有给定 this
值的函数,以及以一个数组(或一个类数组对象)的形式提供的参数。
jsFunction.prototype.myApply = function (thisArg, args) {
if (thisArg) {
thisArg = Object(thisArg);
} else {
thisArg = typeof window !== "undefined" ? window : global;
}
let result;
if (!args) {
result = thisArg._fn();
} else {
result = thisArg._fn(...args);
}
delete thisArg._fn;
return result;
};
备注
虽然这个函数的语法与 call()
几乎相同,但根本区别在于,call()
接受一个参数列表,而 apply()
接受一个参数的单数组。
本文作者:叶继伟
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!