What is the meaning of addHorizonLoadEvent(function() {...})
in the following snippet?
addHorizonLoadEvent(function() {
show_hide_datepickers();
});
What is the meaning of addHorizonLoadEvent(function() {...})
in the following snippet?
addHorizonLoadEvent(function() {
show_hide_datepickers();
});
addHorizonLoadEvent
is a higher-order function -- that means it doesn't expect a non-function value (like a number, or a string) as it's argument, but another function. So the first argument is a new function, that will be called by the original function at some point (often at the end as a callback).
function() {...}
is an anonymous function -- a way to express a function without binding it to a name.
In fact you give a function as parameter of addHorizonLoadEvent.
You could do that :
var fooBar = function() {
//alert("Alert2");
show_hide_datepickers();
});
addHorizonLoadEvent(fooBar);
And the function parameter you give to addHorizonLoadEvent will be used only one time, no need to give it a name, so it is an anonymous function, directly declared in ().
This pattern is a called anonymous function.
function_name(function() {
// Code
});
Is the same as writing
function my_function() {
// Code
}
function_name(my_function);
Meaning that my_function
is passed as an argument to the function function_name
.