1

This function is a click handler.

const select = num => () => value += num;

It has 2 arrows !!

  1. Why? Could you use: const select = (num) => value += num;

if not, why not.

  1. How would const select = num => () => value += num; ? look as a regular function
PRSHL
  • 1,359
  • 1
  • 11
  • 30
  • It's a function that returns a function, technically it's probably a click handler _factory_, with the function it returns being the click handler. – jonrsharpe Aug 12 '21 at 11:50
  • Also called high order function – Raz Luvaton Aug 12 '21 at 11:51
  • 1
    If it is a click handler, then it is likely added like `.addEventListener('click', select(5))`, where the `select` function is invoked _immediately_ and the returned function `() => value += num` becomes the actual event handler. – Ivar Aug 12 '21 at 11:54
  • It should probably be `const select = num => value => value += num;` – Roberto Zvjerković Aug 12 '21 at 12:00
  • 1
    @RobertoZvjerković `+=` on a parameter that isn't used anywhere else doesn't make much sense. I expect that `value` is part of the lexical scope. – Ivar Aug 12 '21 at 12:22
  • 1
    @Ivar You're right, I thought it was `value + num` – Roberto Zvjerković Aug 12 '21 at 12:30
  • After some research: 'code' const select = num => () => value += num; as a regular function looks like this const select = function(num) { return function() { value += num; }; } – Robert Kendrick Aug 13 '21 at 19:55

0 Answers0