call重写
Function.prototype.call = function call(ctx, ...params) {
if(ctx == null) {
ctx =window;
}
if(/!/^(object|function)$/.test(typeof ctx)) {
ctx = Object(ctx);
}
let self = this,
key = Symbol('KEY'),
result;
ctx[key] = self;
result = ctx[key](...params);
delete ctx[key];
return result;
}
function fn(x, y) {
console.log(x)
return x + y
}
const obj = {
name: 'obj'
}
fn.call(obj, 10, 20)
重写 bind
Function.prototype.bind = function(ctx, ...params) {
let self = this;
return (...args) {
params = params.concat(args);
return self.call(ctx, params);
}
}