Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain "you can have functions that change other functions"

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?
like image 683
kchak Avatar asked Jul 25 '15 03:07

kchak


People also ask

How do you call a change in a function?

Go to "Custom attributes" of the field configuration area -> orange arrow bellow and add something like onchange="yourfunction(this)"; For e.g. onclick event , change first part to onclick. This will call your function with element passed to it when value changes (is clicked when event is onclick).

How do you use a value from a function in another function?

If the return value of a function is only going to be used as an input parameter of another function, you can pass the return value directly to the function parameter. This is done by putting the function call in the parameters list of the other function call, just like you would a variable.

Can function be redefined?

It is indeed possible to redefine a function from its body. The technique is used in the so-called Lazy Function Definition Pattern. This function stores the Date of the first call, and returns this Date afterwards.

Which of these higher order functions will execute a provided function once on each element in an array?

forEach() Method. The . forEach() method executes a callback function on each of the elements in an array in order.


2 Answers

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.

like image 95
Wyzard Avatar answered Sep 28 '22 04:09

Wyzard


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.

like image 39
Richard Kho Avatar answered Sep 28 '22 04:09

Richard Kho