1

Could someone explain the follow JavaScript's syntax:

const result = (x, y) => (z) => { 
  
  /* code area */

  return something;
}

My assumptions:

  1. (x, y) => (z) is a call to function z
  2. (z) => { } is a call to whatever is inside { }

Questions:

  1. If so, what would I expect to the code's flow?

    e.g: z is called first then it returns values to the anonymous function (x, y) => ?

  2. Is the parentheses required?

  3. About accessing the variables x, y and z in the code area, is it possible?

Finally, could someone, please, give a basic example how to use/call this syntax.

If I missed something, you can find the full code (my syntax is a made up example) here or just ask me.

Thanks in advance!

Community
  • 1
  • 1
Pedro Gabriel Lima
  • 1,122
  • 1
  • 9
  • 24

2 Answers2

3

It's basically the same thing as this snippet of code:

const result = function(x, y) {
    return function(z) {
        // do stuff
    };
};

The handling of this is not the same as they are arrow functions but besides that it's the same.

So when you call a first time it will return you a function but the arguments are caught inside of the closure so you can use them inside of the function.

const partiallyApplied = result(3,4);
const value = partiallyApplied(7);
Axnyff
  • 9,213
  • 4
  • 33
  • 37
0

This is an Arrow Functions syntax from ES6 spec https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions . Translating it to the classic ES5 syntax will look like:

function (x, y) {
  return function (z) {
    /* code area */

    return something;
  }
}

The variables x, y and z are accessable at the code area of course. The only difference in this case is that arrow functions don't have their own context, so this will refer to a parents' context (or window, if there is no any)

Roman Maksimov
  • 1,537
  • 1
  • 9
  • 14