9

I was reading through Eloquent JavaScript, when I came across this in chapter 5. :

you can have functions that create new functions.

function greaterThan(n) {
  return function(m) { return m > n; };
}
var greaterThan10 = greaterThan(10);

And you can have functions that change other functions.

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

My questions are:

  • How are the above two examples different?
  • How does noisy change Boolean?
2
  • 2
    I think that's just poorly worded. I think the point it's trying to make is that you can use callbacks by passing a function to another function. The "change" involved is really that it's consuming the result of the callback and modifying it. Commented Jul 25, 2015 at 3:36
  • 1
    "change other functions" appears misleading, even if it is trying to 'simply' the operation concept; the function is the same. It would be better expressed as "And you can have functions that use (by wrapping) other functions.." Commented Jul 25, 2015 at 3:43

2 Answers 2

4

The difference is that the argument to noisy is meant to be another function, rather than a "plain" value like a number. So, yes, it creates a new anonymous function just like greaterThan does, but it's a wrapper around an existing function that modifies its behavior.

In this case, the wrapper just logs some messages before and after calling the original function f. But you could do other things, like modifying its arguments or its return value. For example, you can implement partial function application, which lets you provide some of the arguments for a function call at one point in the program, and "remember" those arguments in a new function that takes just the remaining arguments later.

Sign up to request clarification or add additional context in comments.

1 Comment

It's hard consider this the correct answer until it makes clear that saying "changes the function" is somewhere between misleading and just wrong.
1

How are the above two examples different?

greaterThan accepts a parameter, n, which is intended to be a number.

noisy accepts a parameter, f, which is intended to be a function that it can then call within it.

Where greaterThan only evaluates a number, noisy is much more flexible since it can accept any function and execute it.

How does noisy change Boolean?

noisy returns an anonymous function that evaluates Boolean in the line that it stores its results in a variable called val.

4 Comments

So thus noisy does not change Boolean (or the function it originally referred to)..
Correct! It does not change it.
So then what is the quote trying to say?
noisy(Boolean)(0) executes noisy, with Boolean as the f parameter and 0 as the parameter passed in to Boolean. 0 always returns falsy. If this doesn't answer your question, I'm afraid you're not articulating it properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.