2

Possible Duplicate:
In JavaScript, does it make a difference if I call a function with parentheses?

what is the difference between this:

$("a").on("click", anotherFunction);

and this:

$("a").on("click", anotherFunction());

And is there a way to not make the last one self executable without anonymous function?

Community
  • 1
  • 1
ilyo
  • 35,851
  • 46
  • 106
  • 159
  • 2
    I think you want this: `$('a').on('click', anotherFunction).triggerHandler('click');`. So the function becomes the click-handler *and* is also invoked immediately. – Šime Vidas Jun 08 '12 at 19:35

5 Answers5

3

The first one passes the actual function; the second passes the returned value of the function.

I'm unclear on the meaning of your final question.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
1

anotherFunction is a reference to a function. anotherFunction() is that function evaluated with no arguments (and therefore has no reference to anotherFunction). The only reason you'd want to call a function to be passed where a function is expected is if that function returned another function.

dbkaplun
  • 3,407
  • 2
  • 26
  • 33
  • "and therefore has no reference to `anotherFunction`" - that's not strictly true, since `arguments` still exists even if there are no arguments, and `arguments.callee` is the function. – Niet the Dark Absol Jun 08 '12 at 19:33
1

anotherFunction on its own is a reference to the function itself.

anotherFunction() calls the function and results in whatever the function returns.

This is an extremely massive difference.

It's like the difference between:

function test() {
    alert("Hello!");
}
setTimeout(test,1000); // called after one second
setTimeout(test(),1000); // called immediately, timeout fails.
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • No, in that one `anotherFunction` will be called only when the link is clicked. The other example would call the function once and nothing would happen if you clicked the link. – Niet the Dark Absol Jun 08 '12 at 19:39
  • Oops! sorry, my mistake. I wanted to know `setTimeout(test(),1000);` will fail the timeout but if for example `somefunction('arg', callback())`, is that possible the callback would run before the main function finishes executing ? – The Alpha Jun 08 '12 at 19:44
  • Yes, callback will run *before* somefunction, and its return value will be used as a parameter. – Niet the Dark Absol Jun 08 '12 at 19:50
0

You should use the first one. This is because the () execute the function defined before them. If you use the first one, the callback anotherFunction is only called when you want it to. JQuery would do something like anotherFunction (in a var named callback) callback()

Max Snijders
  • 374
  • 2
  • 10
0

The difference is between passing function reference and calling that function.

The first case (probably correct) means: call this function when event occurs.

The second will be interpreted as: call this function immediately and use whatever was returned as a callback. It'll better be a function that I can call later.

The latter case will only work if the function is defined as follows:

function anotherFunction() {
    return function() {
        //real handler
    }
}
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674