Why should we use anonymous functions with jQuery instead of the function directly?

javascript, jquery

Solution

It's working but you need to pass only the function reference (name) like this :

function test (e) {
    console.log('test ok');
}
$('body').on('click', test);

Problem

Some jQuery methods expect a function as a parameter, but to work they should receive an anonymous function as a parameter rather than a function directly, as in the following example: ``` $("a").on("click", function () { retornaNada(); }); ``` rather than ``` $("a").on("click", retornaNada()); ``` Consider `retornaNada()` as a function without any body of code. Why can not we pass the function directly?

Original source