apply、call、bind
call
js
Function.prototype._call = function (target, ...args) {
target = target || window;
const key = Symbol();
target[key] = this;
const res = target[key](...args);
delete target[key];
return res;
};
apply
js
Function.prototype._apply = function (target, args) {
target = target || window;
const key = Symbol();
target[key] = this;
const res = target[key](...args);
delete target[key];
return res;
};
bind
js
Function.prototype._bind = function (target, ...args) {
target = target || window;
const key = Symbol();
target[key] = this;
return function (...innerArgs) {
const res = target[key](...args, ...innerArgs);
// delete target[key]; // 不能删除,否则第二次调用的时候,就会出现问题。
return res;
};
};