call 簡易實(shí)現(xiàn)
標(biāo)簽:
JavaScript
//call Function.prototype.mycall=function(context){ if(typeof this!=='function'){ throw new TypeError('Error') } context=context||window context.fn=this//给 context 创建一个fn属性,并将值设置为需要调用的函数 const args=[...arguments].slice(1)//拆分参数 const result=context.fn(...args) delete context.fn//删除属性 return result }
//apply Function.prototype.myapply=function(context){ if(typeof this!=='function'){ throw new TypeError('Error') } context=context||window context.fn=this if (arguments[1]) { result = context.fn(...arguments[1]) }else { result = context.fn() } delete context.fn//删除属性 return result }
//bind Function.prototype.mybind=function(context){ if(typeof this!=='function'){ throw new TypeError('Error') } const _this=this const args=[...arguments].slice(1) return function F(){ //new 调用 if(this instanceof F){ return new _this(...args,...arguments) } return _this.apply(context,args.concat(...arguments))}}
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦