#手写 call apply bind
⬇️⬇️⬇️ call ⬇️⬇️⬇️
Function.prototype.myCall = function (instanse, ...args) {
let Context = { ...instanse, that: this }
Context.that(...args)
}
function ttt(aaa) {
console.log(this.count)
console.log(aaa)
}
let obj = {
count: 10,
}
ttt.myCall(obj, 11)
⬇️⬇️⬇️ apply ⬇️⬇️⬇️
Function.prototype.myApply = function (instanse, args) {
if (!(args instanceof Array)) throw new Error('is not a Array')
let Context = { ...instanse, that: this }
Context.that(...args)
}
function ttt(aaa) {
console.log(this.count)
console.log(aaa)
}
let obj = {
count: 10,
}
ttt.myApply(obj, [11])
⬇️⬇️⬇️ bind ⬇️⬇️⬇️
Function.prototype.myBind = function (instanse, ...args) {
let Context = { ...instanse, that: this }
return function () {
Context.that(...args)
}
}
function ttt(aaa) {
console.log(this.count)
console.log(aaa)
}
let obj = {
count: 10,
}
const newFun = ttt.myBind(obj, 11)
newFun()