![call, apply, bind的分析與實(shí)現(xiàn)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/31/0f972f57-2ee3-4458-beaa-861da372f3c2/0f972f57-2ee3-4458-beaa-861da372f3c21.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、call, apply, bind的分析與實(shí)現(xiàn)如何轉(zhuǎn)變函數(shù)的this指向 假如是函數(shù)聲明,那么函數(shù)內(nèi)部的this指向全局對(duì)象或者調(diào)用該函數(shù)的對(duì)象。 箭頭函數(shù)的this綁定的是父級(jí)作用域內(nèi)的this 用法call,apply,bind可以綁定this指向。其中bind是永遠(yuǎn)轉(zhuǎn)變this指向并且不會(huì)立刻執(zhí)行。apply,call暫時(shí)轉(zhuǎn)變this指向并且立刻執(zhí)行。 apply,call不同之處在于調(diào)用的參數(shù)形式: call(thisarg, arg1?, arg2?, arg3?)第一個(gè)參數(shù)代表this指向,剩下的參數(shù)就是原函數(shù)的參數(shù)。 apply(thisarg, argarray?)第一個(gè)參數(shù)代
2、表this指向,其次個(gè)參數(shù)是以數(shù)組的形式傳遞參數(shù)。 /* bind, call, apply調(diào)用示例 */ var obj = x: &39;obj&39;, getx: function() / 注重這里不能用法箭頭函數(shù) return this.x; ; / 調(diào)用 obj 對(duì)象上的函數(shù)屬性, this就會(huì)綁定為obj / &39;obj&39; console.log(obj.getx(); var x = &39;window&39; function target(a, b, c) console.log(a, b, c); return this.x; /* bind */ / und
3、efined undefined undefined / &39;window&39; console.log(target(); const targetbound = target.bind(obj, 1); / 此時(shí)targetbound就是一個(gè)轉(zhuǎn)變了this指向的target / 1 2 3 / &39;obj&39; console.log(targetbound(2, 3); /* call */ / 4 5 6 / &39;obj&39; console.log(target.call(obj, 4, 5, 6); /* apply */ / 7 8 9 / &39;obj&39
4、; console.log(target.apply(obj, 7, 8, 9); 自定義實(shí)現(xiàn)call,apply,bind 這三個(gè)函數(shù)都有很強(qiáng)的錯(cuò)誤處理功能,如果傳入的thisarg是一個(gè)基本類型,也會(huì)被用法包裝類替換,雖然不會(huì)報(bào)錯(cuò),但是不推舉這樣寫,盡量傳遞復(fù)雜類型的變量作為thisarg。 自定義實(shí)現(xiàn)mybind,mycall,myapply / 先定義一個(gè)函數(shù)將隨意類型都轉(zhuǎn)換成對(duì)象,用于指向this function anytoobj(value) / symbol, bigint 就不推斷了,挺直在default中 switch (typeof value) case &39;boo
5、lean&39;: return new boolean(value); case &39;number&39;: return new number(value); case &39;string&39;: return new string(value); case &39;undefined&39;: return this; case &39;object&39;: if (value = null) return this; / 這里的this就指向了全局對(duì)象 return value; default: return value; /* mybind */ f
6、totype.mybind = function (thisarg, argarray) / 防止浮現(xiàn) const mybind = ftotype.mybind; mybind(); if (typeof this != &39;function&39;) throw new typeerror(&39;mybind must be called on a function&39;); const that = this; / this 就指向 f.mybind() 的 f const thatarg = anytoobj(thisarg); / 處理一下thisarg
7、 const mybound = function (args) / 指定唯一的鍵 const tempkey = symbol(&39;_innerfunction_&39;); thatarg.tempkey = that; / 將 that 函數(shù)賦值給一個(gè)對(duì)象的屬性 let ret; if (this instanceof mybound) / 調(diào)用 mybind 之后返回的是 mybound,如果調(diào)用 new mybound(),就會(huì)進(jìn)這個(gè)分支 ret = new thatarg.tempkey(argarray.concat(args); else / 這里是調(diào)用 mybound()
8、,這樣調(diào)用 tempkey 辦法里的 this 就指向了 thatarg ret = thatarg.tempkey(argarray.concat(args); / 不會(huì)對(duì)對(duì)象造成污染 delete thatarg.tempkey; return ret; ; return mybound; ; /* mycall */ / 與 mybind 相比挺直調(diào)用了 ftotype.mycall = function (thisarg, argarray) if (typeof this != &39;function&39;) throw new typeerror(&39;
9、mycall must be called on a function&39;); const thatarg = anytoobj(thisarg); const tempkey = symbol(&39;_innerfunction_&39;); thatarg.tempkey = this; const ret = thatarg.tempkey(argarray); delete thatarg.tempkey; return ret; ; /* myapply */ / 與 mycall 相比,其次個(gè)參數(shù)希翼是數(shù)組形式,多了個(gè) createlistfromarraylike 用于轉(zhuǎn)化
10、 argarray ftotype.myapply = function (thisarg, argarray) if (typeof this != &39;function&39;) throw new typeerror(&39;myapply must be called on a function&39;); const createlistfromarraylike = function (val) / 同樣缺乏 bigint 的處理,挺直放在 default 中 switch (typeof val) case &39;string&39;: case &3
11、9;number&39;: case &39;boolean&39;: case &39;symbol&39;: throw new typeerror(&39;createlistfromarraylike called on non-object&39;); case &39;object&39;: if (array.isarray(val) return val; if (val = null) return ; return array.from(val); default: return ; ; / 檢測(cè) argarray 的類型 const tempargarray = crea
12、telistfromarraylike(argarray); const thatarg = anytoobj(thisarg); const tempkey = symbol(&39;_innerfunction_&39;); thatarg.tempkey = this; const ret = thatarg.tempkey(tempargarray); delete thatarg.tempkey; return ret; ; / 這樣 mybind,mycall,myapply就完成了,下面舉行測(cè)試 var obj = x: &39;obj&39;, getx: function(a
13、, b, c) console.log(a, b, c); return this.x; var x = &39;window&39; / 1 2 3 / &39;obj&39; console.log(obj.getx(1, 2, 3); / target變成一個(gè)全局函數(shù),this 指向全局對(duì)象 const target = obj.getx; / 1 2 3 / &39;window&39; console.log(target(1, 2, 3); /* mybind */ const targetbound = target.mybind(obj, 1); / 1 2 3 / &39;obj&39; console.log(targetbound(2, 3); /* mycall */
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 施工現(xiàn)場施工防臺(tái)風(fēng)災(zāi)害威脅制度
- 數(shù)字化時(shí)代下的客戶分析與銷售策略
- 現(xiàn)代辦公技術(shù)與應(yīng)用實(shí)踐培訓(xùn)
- 數(shù)學(xué)圖形在兒童智力開發(fā)中的作用
- 科學(xué)實(shí)驗(yàn)教學(xué)對(duì)小學(xué)生綜合素質(zhì)的培養(yǎng)策略
- 項(xiàng)目突發(fā)環(huán)境事件應(yīng)急預(yù)案
- 二手車批發(fā)合作合同協(xié)議
- 個(gè)人向個(gè)人臨時(shí)借款合同模板
- 上海市租賃合同模板及示例
- 不銹鋼期貨電子交易合同
- 典范英語2b課文電子書
- 大數(shù)據(jù)與會(huì)計(jì)論文
- 17~18世紀(jì)意大利歌劇探析
- 微課制作技術(shù)與技巧要點(diǎn)
- β內(nèi)酰胺類抗生素與合理用藥
- 何以中國:公元前2000年的中原圖景
- 第一章:公共政策理論模型
- 中藥審核處方的內(nèi)容(二)
- GB/T 4513.7-2017不定形耐火材料第7部分:預(yù)制件的測(cè)定
- GB/T 10205-2009磷酸一銨、磷酸二銨
- 公司財(cái)務(wù)制度及流程
評(píng)論
0/150
提交評(píng)論