JS手写代码之call, apply
思路
函数的实现其实都借助了点调用。利用第一个参数做个中转,调用完之后删除。
call
Function.prototype.myCall = function(context = windows, ...args) {
context._fn = this
const result = context._fn(...args)
delete context._fn
return result
}
apply
Function.prototype.myApply = function(context = windows, args) {
context._fn = this
const result = context._fn(args)
delete context._fn
return result
}
一个幽默的前端爱好者,记录下自己的心得体会

浙公网安备 33010602011771号