myCall
Function.prototype.myCall = function (context,...args) {
context[this.name] = this;
return context[this.name](...args);
}
测试
const foo = {
name :"Jack",
hi(str){
console.log("this",this)
console.log('hellow ', this.name, str)
}
}
const bar = { name: "Lucy" };
foo.hi.myCall(bar, "call");
// hellow Lucy call
myBind
Function.prototype.myBind = function (context) {
return (...args) => this.call(context, ...args);
}
测试
const foo = {
name :"Jack",
hi(str){
console.log("this",this)
console.log('hellow ', this.name, str)
}
}
const bar = { name: "Lucy" };
foo.hi.myBind(bar,"bind")("bind");
// hellow Lucy bind