0

could anyone explain to me what is this function doing and how does it work? I'm a beginner and I don't get it.

It is from Eloquent JavaScript.

function noisy(f) {
  return (...args) => {
    console.log('calling with', args);
    let result = f(...args);
    console.log('called with', args, ', returned', result);
  }
  return result;
}
noisy(Math.min)(3, 2, 1);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 1
    f is a function and noisy returns a function – DownloadPizza Mar 08 '21 at 15:06
  • 2
    That last `return result;` statement is probably supposed to be inside the returned `=>` function, not outside. – Pointy Mar 08 '21 at 15:06
  • What specifically don't you understand about it? – Julia Mar 08 '21 at 15:08
  • @Pointy It isn't. It's an example from a book – Sylwia Grzegorczyk Mar 08 '21 at 15:08
  • @SylwiaGrzegorczyk trust us, the book is wrong. the second `return` will never be hit! – Jamiec Mar 08 '21 at 15:09
  • [Higher-order functions in Javascript](https://stackoverflow.com/q/23535316) – VLAZ Mar 08 '21 at 15:11
  • Nice find @VLAZ not only is this the exact same example, it's obviously been fixed in a later edition of this book! – Jamiec Mar 08 '21 at 15:12
  • [The book doesn't have a second return](https://eloquentjavascript.net/05_higher_order.html#h_xxCc98lOBK) – VLAZ Mar 08 '21 at 15:12
  • See [What is 'Currying'?](https://stackoverflow.com/q/36314/2873538) – Ajeet Shah Mar 08 '21 at 15:13
  • @SylwiaGrzegorczyk The output you are getting is not because of `return result;` because `result` can not be accessed outside the first return statement. – Manas Khandelwal Mar 08 '21 at 15:14
  • 1
    @Jamiec weirdly, I tried SO's search (I know it's bad) *and* [symbolhound](http://symbolhound.com/?q=function+noisy%28f%29) but didn't find a question about it. I just used a regular search engine to find the reference in the book and got...the other SO question... – VLAZ Mar 08 '21 at 15:14
  • 1
    @Pointy The book is correct. The code copied by her is wrong. [Check This](https://eloquentjavascript.net/05_higher_order.html#h_xxCc98lOBK) VLAZ mentioned it before... – Manas Khandelwal Mar 08 '21 at 15:26

0 Answers0