原生js如何實現(xiàn)call,apply以及bind
1、實現(xiàn)call
步驟:
- 將函數(shù)設(shè)為對象的屬性;
- 指定this到函數(shù),并傳入給定參數(shù)執(zhí)行函數(shù);
- 執(zhí)行之后刪除這個函數(shù);
- 如果不傳入?yún)?shù),默認(rèn)指向window;
Function.prototype.mycall = function (context, ...args) { //判斷是否為函數(shù),如果不是函數(shù),則報錯 if (typeof this !== "function") { throw new Error("不是函數(shù)"); } context = context || window; context.fn = this; const res = context.fn(...args); delete context.fn; return res; }
測試代碼:
var name = "李輝", age = 25; var obj = { name: "周果", objAge: this.age, myFun: function (fm, to) { console.log(`名字:${this.name},年齡:${this.age},來自:${fm},去往:${to}`) } }; var person = { name: "弟弟", age: 12, }; Function.prototype.mycall = function (context, ...args) { //判斷是否為函數(shù),如果不是函數(shù),則報錯 if (typeof this !== "function") { throw new Error("不是函數(shù)"); } context = context || window; context.fn = this; const res = context.fn(...args); delete context.fn; return res; } obj.myFun.mycall(person, "成都", "仁壽"); //名字:弟弟,年齡:12,來自:成都,去往:仁壽
2、實現(xiàn)apply
Function.prototype.myApply = function (context, ...args) { //判斷是否為函數(shù),如果不是函數(shù),則報錯 if (typeof this !== "function") { throw new Error("不是函數(shù)"); } context = context || window; context.fn = this; args = args && args[0] || []; const result = context.fn(...args); delete context.fn; return result; }
測試代碼:
obj.myFun.myApply(person, ["成都", "仁壽"]); //名字:弟弟,年齡:12,來自:成都,去往:仁壽
3、實現(xiàn)bind
bind()方法主要就是將函數(shù)綁定到某個對象,bind()會創(chuàng)建一個函數(shù),函數(shù)體內(nèi)的this對象的值會被綁定到傳入bind()中的第一個參數(shù)的值。
方法1:使用apply
Function.prototype.myBind = function () { let self = this; //保存原函數(shù) let context = [].shift.call(arguments); //保存需要綁定的this上下文 let args = [...arguments]; //將傳入的剩余參數(shù)轉(zhuǎn)換成數(shù)組 return function () { //返回一個新的函數(shù) self.apply(context,[].concat.call(args,[...arguments])); } }
ES6簡化一下:
Function.prototype.myBind = function (context, ...args1) { return (...args2) => { //返回箭頭函數(shù), this綁定調(diào)用這個方法的函數(shù)對象 context = context || window; return this.apply(context, args1.concat(args2));//合并參數(shù) } }
方法2:不使用call以及apply
將上面的代碼和js手寫實現(xiàn)apply的代碼合并一下:
Function.prototype.myBind = function (context, ...args1) { return (...args2) => { //返回箭頭函數(shù), this綁定調(diào)用這個方法的函數(shù)對象 context = context || window; context.fn = this; const args = args1.concat(args2); const res = context.fn(...args); delete context.fn; return res; } }
測試代碼:
obj.myFun.myBind(person, "成都", "仁壽")();//名字:弟弟,年齡:12,來自:成都,去往:仁壽
以上就是原生js如何實現(xiàn)call,apply以及bind的詳細(xì)內(nèi)容,更多關(guān)于js實現(xiàn)call,apply以及bind的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Js-$.extend擴(kuò)展方法使方法參數(shù)更靈活
在JS里,我們的方法參數(shù)通常使用JQ的$.extend擴(kuò)展方法來實現(xiàn),感興趣的朋友可以了解下2013-01-01JavaScript中的函數(shù)申明、函數(shù)表達(dá)式、箭頭函數(shù)
js中的函數(shù)可以通過幾種方式創(chuàng)建,具體創(chuàng)建方法通過實例代碼給大家介紹的非常詳細(xì),文中通過例子給大家介紹了函數(shù)聲明和表達(dá)式之間的差別,感興趣的朋友跟隨小編一起看看吧2019-12-12JavaScript程序設(shè)計之JS調(diào)試
這篇文章主要介紹了JavaScript程序設(shè)計中的重要環(huán)節(jié):JS調(diào)試,本文通過一個加法器,介紹JS如何調(diào)試,感興趣的小伙伴們可以參考一下2015-12-12javascript實現(xiàn)數(shù)字倒計時特效
這篇文章主要介紹了javascript實現(xiàn)網(wǎng)頁倒計時數(shù)字時鐘效果,是一款非常實用的javascript倒計時特效,具有一定參考借鑒價值,需要的朋友可以參考下2016-03-03JavaScript獲取圖片像素顏色并轉(zhuǎn)換為box-shadow顯示
這篇文章主要介紹了JavaScript獲取圖片像素顏色并轉(zhuǎn)換為box-shadow顯示的方法,用到了HTML5中的FileReader API和getImageData,轉(zhuǎn)換為的CSS3 box-shadow也要注意瀏覽器的兼容問題,需要的朋友可以參考下2016-03-03JS賦值、淺拷貝和深拷貝(數(shù)組和對象的深淺拷貝)實例詳解
這篇文章主要介紹了JS賦值、淺拷貝和深拷貝,結(jié)合實例形式總結(jié)分析了JavaScript數(shù)組和對象的深淺拷貝相關(guān)概念、原理、操作技巧與使用注意事項,需要的朋友可以參考下2020-03-03JavaScript是如何實現(xiàn)繼承的(六種方式)
大多OO語言都支持兩種繼承方式: 接口繼承和實現(xiàn)繼承 ,而ECMAScript中無法實現(xiàn)接口繼承,ECMAScript只支持實現(xiàn)繼承,而且其實現(xiàn)繼承主要是依靠原型鏈來實現(xiàn),下文給大家技術(shù)js實現(xiàn)繼承的六種方式,需要的朋友參考下2016-03-03