簡體   English   中英

curry和休息和傳播運營商

[英]currying and rest and spread operators

幾年前,我在一篇引人入勝的文章中找到了這段代碼,但是作者沒有解釋它的工作原理。 我知道fn和args1變量來自何處,但是我很難理解args2在整個過程中將包含什么,並且想知道是否有人可以提供見解?

function curry(fn, ...args1) { 
    return (...args2) => { fn(...args1, ...args2); } 
}

curry返回一個函數。 調用該返回的函數時,可以傳遞其自己的參數。 這些由args2引用。

 function curry(fn, ...args1) { console.log("args1 = ", ...args1); return (...args2) => { console.log("args2 = ", ...args2); fn(...args1, ...args2); } } function myFunc(...allArgs) { console.log(...allArgs); } const curried = curry(myFunc, 1,2,3); curried(4,5,6); 

如您所見,當調用fn (對myFunc的引用)時,它將接收所有args1args2 ,因此顯示1 2 3 4 5 6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM