const obj = {
a: 1,
fn: function (num = 1, m = 1) {
console.log(this.a * num * m)
}
}
const obj2 = { a: 22 }
this.a = 3
const t = obj.fn
t()
obj.fn()
obj.fn.apply(obj2, [2])
obj.fn.call(obj2, 4)
obj.fn.bind(obj2)(5)
obj.fn.bind(obj2, 5)()
Function.prototype.myCall = function (context) {
if (typeof this !== 'function') {
throw new Error('Not Function')
}
context = context || window
context.fn = this
arguments[1] ? context.fn(...[].slice.call(arguments, 1)) : context.fn()
delete context.fn
}
obj.fn.myCall()
Function.prototype.myApply = function (context) {
if (typeof this !== 'function') {
throw new Error('Not Function')
}
context = context || window
context.fn = this
arguments[1] ? context.fn(...arguments[1]) : context.fn()
delete context.fn
}
obj.fn.myApply()
Function.prototype.myBind = function (context) {
if (typeof this !== 'function') {
throw new Error('Not Function')
}
context = context || window
let args = arguments[1] ? [].slice.call(arguments, 1) : []
return (...params) => {
this.apply(context, [...args, ...params])
}
}
obj.fn.myBind(obj2, 3)(9)
console