Function.prototype.MyApply = function (context) {
// 判断调用者是否为函数
if (typeof this == "function") {
return
}
// 如果未传参数则为 context
context = context || window
let result
context.fn = this
// 判断是否有参数 ,apply传的参数是数组形式的
if (arguments[1]) {
result = context.fn(...arguments[1])
} else {
result = context.fn()
}
// 将对应的fn删掉
delete context.fn
return result
}
手写apply
于 2023-11-30 16:50:49 首次发布