// apply
Function.prototype.myApply = function (thisArg, args = []) {
thisArg = thisArg || window || global
const fnSymol = Symbol('fn')
thisArg[fnSymol] = this
thisArg[fnSymol](...args)
delete thisArg[fnSymol]
}
// call
Function.prototype.myCall = function (ctx, ...args) {
ctx = ctx || window
const fnSymol = Symbol('fn')
ctx[fnSymol] = this
ctx[fnSymol](...args)
delete ctx[fnSymol]
}
// bind
Function.prototype.myBind = function (ctx, ...args) {
ctx = ctx || window
const fnSymol = Symbol('fn')
ctx[fnSymol] = this
return function(..._args) {
args = args.concat(_args)
ctx[fnSymol](...args)
delete ctx[fnSymol]
}
}
const obj = {
a: 3
}
function f1() {
console.log(this.a)
}
f1.myBind(obj)()
console