2

I have to review one of react script that initially create by company's outsourcing. I accidentally fall in to this block of code and confuse about the multiple arrow function. The block of code is look like this

export const createSectionCreator = getQuestions => (
  id,
  label,
  styleOptions,
  heading
) => (app, prefix) => ({
  id,
  label,
  heading,
  questions: getQuestions(app, prefix, { id, label }),
  styleOptions: _.defaults(styleOptions, {})
})

I know that how below code is worked let sum = x => y => x+y

But I don't understand how the original code is worked

devserkan
  • 16,870
  • 4
  • 31
  • 47
user1727608
  • 51
  • 1
  • 3
  • https://www.sitepoint.com/es6-arrow-functions-new-fat-concise-syntax-javascript/ – abhit Aug 08 '18 at 14:19
  • What do you mean by "how the original code is worked"? – devserkan Aug 08 '18 at 14:24
  • @devserkan He wants to understand how this is working. – Arup Rakshit Aug 08 '18 at 14:24
  • 2
    Read [Currying versus partial application (with JavaScript code)](http://2ality.com/2011/09/currying-vs-part-eval.html) article. You will be set after reading it. And this [Currying](http://2ality.com/2017/11/currying-in-js.html#currying). – Arup Rakshit Aug 08 '18 at 14:25
  • @ArupRakshit, but OP says "I know that how below code is worked". I think, I misunderstood :) – devserkan Aug 08 '18 at 14:26
  • 1
    Looks like this is really just about function currying. To my understanding, `createSectionCreator` is a function that accepts parameter `getQuestions` as parameter, then returns a function that accepts `id, label, styleOptions, heading`, then it returns a function which accepts `app, prefix` parameter which ultimately returns a JSON object containing `id, label, heading, questions, styleOptions`. – ionizer Aug 08 '18 at 14:47
  • 1
    The parentheses just define multiple parameter names per nested functions. Like `let sum = x => (y, z) => x+y+z; sum(1)(2,3)` – Bergi Aug 08 '18 at 14:51
  • I just wonder that what is the benefit gained from this kind of pattern. – user1727608 Aug 09 '18 at 01:06

0 Answers0