Function.prototype.myCall = function (context) {
var context = context || window
var arg = [...arguments].slice(1)
context.fn = this
var result = context.fn(...arg)
delete context.fn
return result
}
Function.prototype.myApply = function (context = window, args) {
let key = Symbol('key')
context[key] = this
let result = context[key](...args)
delete context[key]
return result
}
Function.prototype.myBind = function (context) {
if (typeof this !== 'function') {
throw new TypeError('Error')
}
var _this = this
var args = [...arguments].slice(1)
return function F() {
if (this instanceof F) {
return new _this(...args, ...arguments)
}
return _this.apply(context, args.concat(...arguments))
}
}
let a = {
value: 1
}
function getValue(name, age) {
console.log(name)
console.log(age)
console.log(this.value)
}
getValue.myBind(a, 'poe', '24')()
console