一、call函数的实现
Function.prototype.EMcall = function (thisArg, ...args) {
let fn = this;
thisArg = (thisArg !== null && thisArg !== undefined) ? Object(thisArg) : window;
thisArg.fn = fn;
let result = thisArg.fn(...args);
delete thisArg.fn;
return result;
}
二、apply函数的实现
Function.prototype.EMapply = function (thisArg, argArray) {
var fn = this;
thisArg = (thisArg !== null && thisArg !== undefined) ? Object(thisArg) : window;
thisArg.fn = fn;
let result = null;
argArray = argArray || [];
result = thisArg.fn(...argArray);
delete thisArg.fn;
return result;
}
三、bind函数的实现
Function.prototype.EMcall = function (thisArg, ...args) {
let fn = this;
thisArg = (thisArg !== null && thisArg !== undefined) ? Object(thisArg) : window;
function proxyFn(...argArray) {
thisArg.fn = fn;
let finalArgs = [...args, ...argArray];
let result = thisArg.fn(...finalArgs);
delete thisArg.fn;
return result
}
return proxyFn
}