簡體   English   中英

我已經閱讀了很多資源,並試圖找到我的問題的答案,但沒有成功。 我在問如何鏈接高階函數

[英]I have read many resources and tried to find answer to my question but couldn't. I'm asking about how chaining the higher order functions work

所以,這是簡單的箭頭 function,但我不明白 sth、sth1 等如何工作或者它們是什么? 它們是 arguments 還是 function 還是只是值? 或者可以是他們兩個? 如果是這樣,這個鏈接是如何工作的,請幫助我。

const someFunction = sth => sth1 => sth2 => {
  // do something if needed
}

你所擁有的並不是真正的鏈接箭頭函數(或currying ),而是一種將相同的 function 一次分配給多個標識符的非常混亂的方式。

=從右到左求值,整個表達式求值為右側的值。 賦值的最右邊是箭頭 function 表達式:

sth2 => {
  // do something if needed
}

在那里, sth2是一個參數。

箭頭 function 被分配給一個名為sth1的標識符:

sth1 = sth2 => { ...

sth1還沒有被宣布。 如果您處於嚴格模式,這將拋出錯誤,或者它將在全局 object 上放置一個屬性,在草率模式下箭頭值為 function。

然后,整個部分的計算結果為箭頭 function,並且該過程對sth本身重復。

sth = theArrowFunction

最后,賦值的計算結果為箭頭 function,並創建包含箭頭 function 的標識符someFunction

完成同一件事的一種不那么令人困惑的方法(在嚴格或草率模式下)是

sth1 = sth2 => {
  // do something if needed
};
sth = sth1;
const someFunction = sth1;

但是您確定那是您使用的確切代碼嗎? 如果您的代碼實際上是

const someFunction = sth => sth1 => sth2 => {
  // do something if needed
}

那么這不是鏈式賦值,而是柯里化,其中sthsth1sth2對於不同的函數都是 arguments 。

這相當於:

const myFun = x => {...}
const sth1 = myFun
const sth = sth1
const someFunction = sth

暫無
暫無

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

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