myCall

Function.prototype.myCall = function (context,... args) { context[this.name] = this; return context[this.name](... args); }Copy the code

test

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
Copy the code

myBind

Function.prototype.myBind = function (context) { return (... args) => this.call(context, ... args); }Copy the code

test

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
Copy the code