- call 实现
(function(){
if(!Function.prototype.call) {
Function.prototype.call = function(context=window, ...args) {
if(typeof this !== 'function') {
throw new TypeError("this not a function");
}
context.fn = this;
let result = context.fn(...args);
delete context.fn;
return result;
}
}
})()
- apply 实现
(function(){
if(!Function.prototype.apply) {
Function.prototype.apply = function (context=window, ...args) {
if(typeof this !== 'function') {
throw new TypeError("this not a function");
}
context.fn = this;
let result = args[0] ? context.fn(...args) : context.fn();
delete context.fn;
return result;
}
}
})()