1

It is from a book called Eloquent JavaScript:

function noisy(f) {
  return function(arg) {
    console.log("calling with", arg);
    var val = f(arg);
    console.log("called with", arg, "- got", val);
    return val;
  };
}
noisy(Boolean)(0);
// → calling with 0
// → called with 0 - got false

The chapter is about higher order functions, and this block of code is to show how you can have functions that change other functions. I went through it, trying to understand what happens, but no luck. I would like to understand each line, especially where he declares the variable val.

Thank you.

EDIT: I see it as, function noisy that takes in argument f and returns another function(arg). Why is he declaring val = f(arg); How does that equal false? And why is he calling noisy with two arguments?

Azar_Javed
  • 75
  • 1
  • 2
  • 9
  • I probably would have closed this with something like "Too Broad". Your question isn't really answerable in a single answer - I think you need someone to sit down and explain the concept to you. As the answers unfold, you'll have a cascading series of additional questions. That doesn't really work with SO's format. – JDB Apr 13 '15 at 16:57
  • 1
    @GeorgeStocker - I don't understand why this question is closed. The code is included. An answer explaining how this code works can be provided in 4 to 5 paragraphs of explanation. Why would this be either off-topic or too broad? I've voted to reopen, but my single vote can't seem to overcome George's super powers who could close it in a single vote. – jfriend00 Apr 13 '15 at 17:00
  • 1
    This whole code block is just a fancy way of calling `Boolean(0)`. – gen_Eric Apr 13 '15 at 17:00
  • 1
    Maybe you can describe what about it you do understand, and what part(s) you are having difficulty understanding. – Paul Richter Apr 13 '15 at 17:00
  • 2
    @jfriend00 The title is vague and there's no actual problem statement. No one will search google for "Can someone explain this function example to me?". One of the chief issues on Stack Overflow is making questions useful to others; without more information (Page number, chapter title, section title; and Question Title that would be useful to others), this question is not useful to other people. Also, the 'problem' the user describes (I don't understand it) isn't helpful: We need specific problems. When you run this code, what do you notice that you expect? What do you not expect? – George Stocker Apr 13 '15 at 17:03
  • 3
    @GeorgeStocker - the question is completely clear. Seriously, you're closing this question because you don't think it works well for a search engine? Come on now. It's a perfectly fine question that is completely clear what kind of answer they want. If you want to suggest a better title, give the relatively new user a break and help them rather than just slamming the question closed with no feedback whatsover. They want to know how this code works and what it is supposed to do. Very clear question to me. I'm willing to supply a most excellent answer if the question was open. – jfriend00 Apr 13 '15 at 17:04
  • 1
    @GeorgeStocker - this hostile behavior to new users with no feedback or help offered is what gives StackOverflow a poor reputation in some circles. This is not the type of community anyone wants StackOverflow to be. Offer some comments with suggestions on improving the question and title. Edit it yourself to improve it. Geez. – jfriend00 Apr 13 '15 at 17:07
  • @jfriend00 Something we ask is if you would like to edit a bad question into a good question so you can answer it; do so. That's a win for everyone. – George Stocker Apr 13 '15 at 17:09
  • It's also worthy to note that this could be a duplicate; but without some specificity, there's no way to know if the OP's question has already been answered on Stack Overflow. – George Stocker Apr 13 '15 at 17:10
  • @GeorgeStocker - I don't go investing my time in questions that are closed that I cannot reopen since questions that are closed very rarely ever get reopened (yes it happens, but not very often). If you reopen it (particularly now that the OP has edited it), then I can work on it. – jfriend00 Apr 13 '15 at 17:11
  • @jfriend00 If you edit it, it's placed in the re-open queue, so it is in your power to make it better to have it reopened. – George Stocker Apr 13 '15 at 17:11
  • 1
    @GeorgeStocker - the OP has already been editing the question. Why don't YOU offer them some feedback on what else needs to be improved before it's acceptable to you. You're the one with the uber power here and the judgement that it was not acceptable, not me. – jfriend00 Apr 13 '15 at 17:12
  • duplicate of [How do JavaScript closures work?](http://stackoverflow.com/q/111102/1048572) and many other similar questions. – Bergi Apr 13 '15 at 17:13
  • 3
    https://stackoverflow.com/search?q=eloquent+noisy !!! – Bergi Apr 13 '15 at 17:15
  • @Bergi Somehow I completely missed it, I guess my google-foo isn't as strong. Sorry for the confusion, English is not my main language. – Azar_Javed Apr 13 '15 at 17:18
  • @George Stocker Should I delete this question now? – Azar_Javed Apr 13 '15 at 17:19
  • @Azar_Javed I don't see a reason to if you don't want to. – George Stocker Apr 13 '15 at 17:28

0 Answers0