4 Answers 4
We know that an expression of the form f(x) is a call to the function f taking a single argument x. In addition, JS allows us to pass function callbacks as parameters to other functions. For instance:
function callback(x) { alert(x); }
c(callback, 5);
The callback takes a single argument, and that other argument 5 is passed as a parameter to the callback. In turn, it alerts the number.
5 Comments
In this case c is a function that can be passed two objects, a and b. From the definition it looks like a should be a function object and b a parameter for that function.
Within the definition you have a function called a being passed an argument of the object b.
Find out more about functions in javascript.
2 Comments
c and it would run it. In your example here in the comments, the only function that can be run is the one defined inside the function definition for c. One is not better than the other, it depends on the use case.This means that a should be a function which gets executed when it's passed to c, along with another argument.
//c accepts a function and an argument
c(function(theSentB){
//this passed function gets executed due to a(b);
//and 'hello' is passed to it
alert(theSentB);
},'hello')
Comments
JavaScript treats functions as a first class objects. This means that you can pass function as parameters. (it is a little like delegates in C#)
In your case, a is a parameter that points to a function and you execute it inside the function represented by c . (a itself, using b as it's first argument).
()as a postfix operator, that can be applied to anything that has a value.