views:

70

answers:

3

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  A: 

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.

gilly3
how do I replace callback for the anonymous one inside the loop?
Richard
`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`.
gilly3
+2  A: 

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(...).

pkaeding
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])
Richard
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).
pkaeding
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?
Richard
note on the side: arguments is an array, which are zero based so it should be arguments[3](arguments[i])
Andre Haverdings
@Andre good catch; you are right.
pkaeding
@Richard, I added some more clarification. Does that help explain how the anonymous function is called?
pkaeding
@pkaeding, I understand it now. Thanks everybody!
Richard
A: 

Regarding function concept in javascript:

Functions in JavaScript are objects. Which means that a variable can hold a reference to a function and that function can be invoked through that variable. For example:

function my_sum(a, b) { return a + b }
var my_sum_function = my_sum;

// call this function through variable 
alert(my_sum_function(1, 3));

So, in your example anonymous function is created and assigned to variable callback is assigned a reference to that anonymous functions. Inside multiplyByTwo, anonymous function is called through that variable.

Tumas
The question is about the second part, the function that is passed without a name or attached to a variable.
Richard
It relates. Function is created when multiplyByTwo is called and reference to that functions is assigned to callback variable.
Tumas
it kinda is attached to a variable.. the argument of the function multiplyByTwo
Andre Haverdings
I also think your confused a bit.. javascript is evaluated not compiled.. the function is created when the parser gets to the line multiplyByTwo(1, 2, 3, function(a){return a + 1}); and then attached to the arguments
Andre Haverdings
sorry, the quarter is about to drop for me, or the light is switched on for me. I can see my own confusion now. It is totally clear. The anonymous function is passed as a value and interpeted by callback. I think everyone has answerd the question already.
Richard