0

I am reading the book Eloquent to learn javascript. I came across this example

function unless(test, then) {
    if (!test) then();
}

function repeat(times, body) {
    for (var i = 0; i < times ; i++) body(i);
}

repeat(3, function (n) {
    unless(n % 2, function () {
        console.log(n, " is even ");
    });
});

// → 0 is even
// → 2 is even

I know functions can be passed as arguments and can be inside one another. But are then() and body() functions? Where are they defined? What's the value of n?

3
  • then and body are just the names given to the arguments. The code assumes they are functions (and will fail nastily if you pass them something else) and calls them. In this case, since they are functions, it works just fine. They are defined in the argument list of the call itself. Commented May 18, 2016 at 15:15
  • what's the value of n? and if they're declaring " function ( n ) ",isn't the function supposed to have a name? same with the parameter of unless! Commented May 18, 2016 at 15:18
  • "what's the value of n?" Whatever is passed to the function. Look at where the function is called. "isn't the function supposed to have a name" No, functions don't have to have a name. How parameters work doesn't suddenly change when you pass a function. Simplest example: function foo(n) { console.log(n); }; foo(5); foo(function() {});. In the first call, n will have the value 5, in the second call n will refer to a function. Commented May 18, 2016 at 15:25

3 Answers 3

3

but are then() and body() functions?

then and body are parameters. Their values depend on the values that are passed to unless and repeat. So in order to determine what the values are (i.e. whether they are functions or not) you need to look at where unless and repeat are called and which values are passed to them.

Keep in mind that, in general, repeat and unless can be called multiple times. The values of then and body can be different for every function call, but they will have to be of the type (number, string, function, ...) the function expects, otherwise it won't work correctly.

Where are they defined?

In your example, repeat and unless are called here:

repeat(3, function (n) {
    unless(n % 2, function () {
        document.write(n + " is even<br>");
    });
});

You can see that the second arguments passed to repeat and unless are indeed functions.

What's the value of n?

Lets have a look where the function is called. The function is passed as second argument to repeat. repeat uses body to refer to the second argument and it laters calls it as body(i) in a loop. i will have the values 0 to times - 1. times is the first argument passed to repeat which in your example is 3. So the function will be called multiple (three) times, receiving the values 0, 1 and 2.

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

1 Comment

Was just about to post something like that, perhaps worth to mention that if he is familiar with other languages he may interprete "body" and "then" as anonymous functions? upvote anyway, well explained and clearly answers to the op's questions.
0

Here is a little explanation.

function unless ( test , then ) {
  if (! test ) then () ;
}
function repeat ( times , body ) {
  for ( var i = 0; i < times ; i ++) body ( i ) ;
}

// in here you also pass 2 params to repeat.
// 3 and an anonymous function, the function is the argument 'body' in repeat
repeat (3 , function ( n ) {
  // as you can see here - you pass 2 params to unless, 
  // 1 is the result of n %2, and an anonymous  function, the function is the argument
  // "then" in unless.
  unless ( n % 2 , function () {
    console . log (n , " is even ") ;

  }) ;
}) ;

the value of n is changing from 0 to 3 it, its going to be 0,1,2

Also since your a beginner i would suggest using https://jsfiddle.net/ to test stuff out.

Comments

0

In your example here you have the signature of the unless function:

function unless ( test , then ) {
// code here
}

both test and then are called parameters and change depending on the arguments you pass when you call unless

for example :

 function first(){}
 function second(){}

 unless(first,second)

means that test inside the unless is a function cause test is replaced with first function and then is replaced with the second function when calling unless

// unless(test,then) => unless(first,second)
//         |     |              |        |
//         |-----|---------------        |     
//               ------------------------|

Comments

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.