javascript - js中call函數(shù)的用法?
問(wèn)題描述
var currying = function(fun) { //底下這句代碼是什么意思? var args = Array.prototype.slice.call(arguments, 1); return function() { //底下這句代碼也不怎么清楚 var _args = args.concat(Array.prototype.slice.call(arguments)); return fun.apply(null, _args); };}
能解釋一下這個(gè)函數(shù)的代碼的意思嗎?我百度過(guò)call方法與 slice方法,但是結(jié)合起來(lái)用,并且加上函數(shù)的參數(shù)arguments對(duì)象,我就搞不清楚了,新手學(xué)習(xí),所以有些概念不怎么理解
問(wèn)題解答
回答1:簡(jiǎn)單的例子 var aa=[1,2,3],bb={0:1,1:2,2:3,length:3}; aa.slice(1)//[2,3],此時(shí)的slice上的this就是指的aa的 //bb是對(duì)象沒有slice方法,又想得到[2,3]該怎么辦? aa.slice.call(bb,1)//[2,3] 回答2:
別百度了,直接mdn看吧 https://developer.mozilla.org...
var args = Array.prototype.slice.call(arguments, 1);
arguments 是一個(gè)類數(shù)組對(duì)象,而非數(shù)組,不一定有數(shù)組的slice方法,所以用call方法來(lái)使得arguments對(duì)象能跟數(shù)組一樣調(diào)用slice方法。
相關(guān)文章:
1. 數(shù)據(jù)庫(kù) - MySQL 單表500W+數(shù)據(jù),查詢超時(shí),如何優(yōu)化呢?2. python - Django分頁(yè)和查詢參數(shù)的問(wèn)題3. 求大神幫我看看是哪里寫錯(cuò)了 感謝細(xì)心解答4. MySQL客戶端吃掉了SQL注解?5. android - Windows系統(tǒng)下運(yùn)行react-native App時(shí),報(bào)下面的錯(cuò)誤?6. javascript - 圖片能在網(wǎng)站顯示,但控制臺(tái)仍舊報(bào)錯(cuò)403 (Forbidden)7. php自學(xué)從哪里開始?8. mysql - AttributeError: ’module’ object has no attribute ’MatchType’9. javascript - JS設(shè)置Video視頻對(duì)象的currentTime時(shí)出現(xiàn)了問(wèn)題,IE,Edge,火狐,都可以設(shè)置,反而chrom卻...10. phpstady在win10上運(yùn)行
