const A = {
name: 'A',
say(prefix, age){
console.log(`${prefix},my name is ${this.name}, i'm ${age} years old`);
}
}
const B = {
name: 'B'
}
Function.prototype.myCall = function(target, ...args){
const _target = target || window;
console.log(this)
const symbolKey = Symbol();
_target[symbolKey] = this;
const res = _target[symbolKey](...args)
delete _target[symbolKey]
return res
}
A.say.myCall(B, 'hello', 12)
Function.prototype.myApply = function(target, args){
const _target = target || window;
const symbolKey = Symbol();
_target[symbolKey] = this;
const res = _target[symbolKey](...args)
delete _target[symbolKey]
return res
}
A.say.myApply(B, ['goodMorning', 14])
Function.prototype.myBind = function(target, ...args){
const _target = target || {};
const symbolKey = Symbol();
_target[symbolKey] = this;
return function(...innerArgs){
const res = _target[symbolKey](...args, ...innerArgs)
return res;
}
}
const bsay = A.say.myBind(B, 'hello')
bsay(18)
console