JavaScript 手寫call apply bind

相同點(diǎn):call apply bind都是為了改變函數(shù)的this指向
不同點(diǎn):
1)call 參數(shù)依次傳入
2)apply 參數(shù)以數(shù)組的方式傳入
3)bind 是一個(gè)方法,需要用()才能執(zhí)行,兩種傳參方法都可以

obj.myFun.call(db,'成都','上海');  // 德瑪 年齡 99  來自 成都去往上海
obj.myFun.apply(db,['成都','上海']);  // 德瑪 年齡 99  來自 成都去往上海  
obj.myFun.bind(db,'成都','上海')();    // 德瑪 年齡 99  來自 成都去往上海
obj.myFun.bind(db,['成都','上海'])();  // 德瑪 年齡 99  來自 成都, 上海去往 

手寫實(shí)現(xiàn)#

call

    Function.prototype.myCall = function(context) {
        if(typeof this !="function"){
            throw new TypeError()
        }
        //如果沒有傳參,綁定window
        context=context||window
        // 改變this的指向
        context.fn=this
        //獲得參數(shù)
        const args=[...arguments].slice(1)
        //執(zhí)行這個(gè)方法
        const result=context.fn(...args)
       //刪除我們聲明的fn屬性 
        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
        let result
        // 處理參數(shù)和 call 有區(qū)別
        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)
        // 返回?個(gè)函數(shù)
        return function F() {
            // 因?yàn)榉祷亓?個(gè)函數(shù),我們可以 new F(),所以需要判斷
            if (this instanceof F) {
                return new _this(...args, ...arguments)
            }
            return _this.apply(context, args.concat(...arguments))
        }
    }
    f.say.myCall(fn,"call","y")//namecally
    f.say.myApply(fn,["apply","y"])//nameapplyy
    f.say.myBind(fn,"bind","y")()//fnbindy
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容