1

What is the meaning of addHorizonLoadEvent(function() {...}) in the following snippet?

addHorizonLoadEvent(function() {
    show_hide_datepickers();
});
5
  • 1
    It is an anonymous function Commented Jul 20, 2015 at 8:53
  • 1
    addHorizonLoadEvent: " Added so that we can append Horizon scoped JS events to the DOM load events without running in to the "horizon" name-space not currently being defined since we load the scripts at the bottom of the page." Commented Jul 20, 2015 at 8:54
  • 2
    possible duplicate of Explain JavaScript's encapsulated anonymous function syntax Commented Jul 20, 2015 at 8:59
  • stackoverflow.com/questions/336859/… Commented Jul 20, 2015 at 9:03
  • @NeelabhSingh Could you accept an answer, please? Commented Jul 22, 2015 at 14:43

3 Answers 3

5

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.

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

Comments

1

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.

1 Comment

Is similar to .. but is not "the same". I've linked some relevant questions.
1

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

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.