Function.prototype.callPolyfill= function(){
// 獲取參數(shù)列表
let args = [...arguments]
// 獲取要綁定的that
let that = args[0] || window
// 將原函數(shù)復(fù)制綁定當(dāng)前的that
that.fn = this
// 執(zhí)行復(fù)制綁定that后的函數(shù)
const result = that.fn(...args.slice(1))
// 解綁,防止破環(huán)that
delete that.fn
// 返回結(jié)果
return result
}
Function.prototype.applyPolyfill = function(){
// 獲取參數(shù)列表
let args = [...arguments]
// 獲取要綁定的that
let that = args[0] || window
// 將原函數(shù)復(fù)制綁定當(dāng)前的that
that.fn = this
// 執(zhí)行復(fù)制綁定that后的函數(shù)
args[1] = args[1] || []
const result = that.fn(...args[1])
// 解綁,防止破環(huán)that
delete that.fn
// 返回結(jié)果
return result
}
Function.prototype.bindPolyfill= function(){
// 獲取參數(shù)列表
let args = [...arguments]
// 獲取要綁定的that
let that = args[0] || window
let targetThis = this
return function(){
// 將原函數(shù)復(fù)制綁定到當(dāng)前的that
that.fn = targetThis
const result = that.fn(...args.slice(1),...arguments)
// 解綁,防止破環(huán)that
delete that.fn
// 返回結(jié)果
return result
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。