2

I am reading this function in a book, and I don't understand how the anonymous function is passed the thirst three arguments?

This is what I understand

function multiplyByTwo(a, b, c, callback) {
  var i, ar = [];
  for(i = 0; i < 3; i++) {
    ar[i] = callback(arguments[i] * 2);
  }
  return ar;
}

but not this

myarr = multiplyByTwo(1, 2, 3, function(a){return a + 1});

thanks, Richard

1
  • 1
    in javascript functions are almost like objects, you can define it like this 'var func = function(a,b,c){return a + b + c + 1};' and call it like this 'func(1,2,3)' or this 'func.apply(this, [1,2,3])' Commented Oct 13, 2010 at 22:23

2 Answers 2

2

Some comments detailing the contract of the multiplyByTwo function would be helpful here:

// multiplyByTwo
// num num num (num -> x) -> x[]
function multiplyByTwo(a, b, c, callback) {
  ...

This means that the function takes four arguments, and returns an array of x. x can be any type. The first three arguments are numbers. The fourth argument to the function is actually a function itself. This function takes a number, and returns an x. Again, x can be any type, but it is the same type throughout the contract.

Inside the multiplyByTwo function, the callback function is called once for each of the other arguments. The result of each of these calls is added to an array, which is the return value of multiplyByTwo.

The arguments variable is a special variable in javascript, which, inside the scope of a function, gives access to all of the arguments to that function, as an array.

The callback function is bound to the name callback in the scope of the multiplyByTwo function. It can then be called as any other function, such as by appending parentheses and arguments. The anonymous function that you gave when you called multiplyByTwo can then be referenced by that name.

So, in the example given, multiplyByTwo is called with four arguments: 1, 2, 3, and an anonymous function. Inside the scope of the multiplyByTwo function, these arguments are bound to the variables a, b, c, and callback, respectively. These arguments can also be referenced through the special arguments array. In the example, both methods are used to reference the arguments. The first three are referenced using the arguments array inside a loop, and the fourth argument is referenced using its name, as in callback(...).

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

6 Comments

I understand how callback is used in the loop, but how does it work for the anonymous one. Do you call that one arguments[4](arguments[i])
You could call the function with arguments[4](arguments[i]), but you can also call it by the name callback, since you bound it to that name by naming the arguments (well, you named the first four arguments anyway).
The first one was an example with a function attached to a variable.The second part should execute the same way according to the book, but it diddn't gave an example how the anonymous function was being called?
note on the side: arguments is an array, which are zero based so it should be arguments[3](arguments[i])
@Richard, I added some more clarification. Does that help explain how the anonymous function is called?
|
1

The anonymous function is called 3 times in the loop, each time passing in a different argument. The first time it passes in arguments[0], which is a (and arguments[1] would be b, etc.). arguments is a property of the currently executing function and refers to the parameters passed into the function. In JavaScript, you don't have to name your parameters, or access them by their names, or even pass in all of the named parameters.

2 Comments

how do I replace callback for the anonymous one inside the loop?
callback is a reference to the anonymous function. Since the 4th parameter is named callback, it refers to the 4th parameter passed in, which is a function. As such, your once anonymous function is now named callback within the scope of multiplyByTwo.

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.