call()函数
Function.prototype.myCall = function () {
let args = [...arguments];
let obj = args.shift() || window;
obj.fn = this;
const result = obj.fn(...args);
delete obj.fn;
return result;
};
apply()函数
Function.prototype.myApply = function () {
let args = [...arguments];
let obj = args.shift() || window;
obj.fn = this;
let result;
if (args[0]) {
result = obj.fn(...args[0]);
} else {
result = obj.fn();
}
delete obj.fn;
return result;
};
bind()函数
Function.prototype.myBind = function () {
const args = Array.prototype.slice.call(arguments);
const t = args.shift();
const self = this;
return function () {
return self.apply(t, args);
};
};