zlx362211854 / daily-study

每日一个知识点总结,以issue的形式体现
10 stars 6 forks source link

122. 模拟实现 call 方法 #179

Open goldEli opened 4 years ago

goldEli commented 4 years ago

模拟实现 call 方法

goldEli commented 4 years ago
const dog = {
  name: "xiaohuang",
  say: function(msg) {
    console.log(`${this.name}: ${msg}`)
  }
}
const cat = {
  name: "mongo"
}
Function.prototype.mycall = function(target, ...params) {
  target.fn = this
  target.fn(...params)
  delete target.fn
}
dog.say.mycall(cat, "hi") // mongo: hi