// call
Function.prototype.ccall = function(thisArg, ...args){
let bindThis = (thisArg !== null && thisArg !== undefined) ? Object(thisArg) : window
bindThis.fn = this
let res = bindThis.fn(...args)
delete bindThis.fn
return res
}
// apply
Function.prototype.capply = function(thisArg, args){
let bindThis = (thisArg !== null && thisArg !== undefined) ? Object(thisArg) : window
bindThis.fn = this
let res = bindThis.fn(...args)
delete bindThis.fn
return res
}
// bind
Function.prototype.cbind = function(thisArg, ...args){
let bindThis = (thisArg !== null && thisArg !== undefined) ? Object(thisArg) : window
return (...secondArgs) => {
bindThis.fn = this
let argsArr = [...secondArgs, ...args]
let res = bindThis.fn(...argsArr)
delete bindThis.fn
return res
}
}
手写call apply bind
于 2023-02-08 21:15:17 首次发布