- call
Function.prototype.Mycall = function(target, ...args) {
target = target || window
const symbolKey = Symbol()
target[symbolKey] = this
const res = target[symbolKey](...args)
delete target[symbolKey]
return res
}
- apply
Function.prototype.Myapply = function(target, args) {
target = target || window
const symbolKey = Symbol()
target[symbolKey] = this
const res = target[symbolKey](...args)
delete target[symbolKey]
return res
}
- bind
Function.prototype.Mybind = function(target, ...outArgs) {
target = target || {}
const symbolKey = Symbol()
target[symbolKey] = this
return function(...innerArgs) {
const res = target[symbolKey](...outArgs, ...innerArgs)
return res
}
}