手写call
Function.prototype._call = function (cs, ...args) {
const o = (cs === undefined) ? window : Object(cs);
const key = Symbol();
o[key] = this;
const result = o[key](...args);
delete o[key];
return result;
};
};
手写apply
Function.prototype._apply = function (cs, array = []) {
const o = (cs === undefined) ? window : Object(cs);
const key = Symbol();
o[key] = this;
const result = o[key](...array);
delete o[key];
return result;
手写bind
Function.prototype._bind = function(ctx, ...args) {
const _self = this
const newFn = function(...rest) {
return _self.call(ctx, ...args, ...rest)
}
if (_self.prototype) {
newFn.prototype = Object.create(_self.prototype);
}
return newFn
}