// 思路:将要改变this指向的方法挂到目标this上执行并返回
Function.prototype.myapply = function (context) {
if (typeof this !== 'function') {
throw new TypeError('not funciton')
}
context = context || window
context.fn = this
let result
if (arguments[1]) {
result = context.fn(...arguments[1])
} else {
result = context.fn()
}
delete context.fn
return result
}
本文介绍了一种自定义实现Function.prototype.myapply的方法,通过将目标函数挂载到指定上下文并执行,从而改变函数调用时的this指向。代码示例展示了如何处理不同参数情况下的函数调用。
310

被折叠的 条评论
为什么被折叠?



