I noticed a difference when calling a function with empty parentheses, or without any parentheses at all. However, I am not passing any arguments to the function so I wondered, what would be the difference between:
window.onload = initAll();
and
window.onload = initAll;
Please explain the principle behind it.
I'm 6 years late but I feel this could have been explained a lot simpler than the above answers.
So here is the TLDR; or bird's eye view when calling functions using and not using
()
'sLets take this function for example:
if you log "foo" - without
()
Using no
()
means to fetch the function itself. You would do this if you want it to be passed along as a callback.if you log "foo()" - with
()
Using
()
after a function means to execute the function and return it's value.This executes
initAll()
straight away and assigns the function's return value towindow.onload
. This is usually not what you want.initAll()
would have to return a function for this to make sense.this assigns the actual function to
window.onload
- this is possible because in JavaScript, as @Felix says, functions are first class objects - without executing it.initAll
will be executed by the load event.initAll
is a reference to a function value and the brackets operator appended to the function name RUNS this function object.So if you do something like
then
a
will become the same asinitAll
- for example you can doa()
- but withthe variable
a
will get the return value of the executedinitAll
functionFunctions in javascript are first-class citizens, and as such, can be assigned to other variables or passed around as arguments.
So, when you do
You are setting the
onload
property of thewindow
object to reference theinitAll
function itself.When you do
You are setting the
onload
property to hold the return value of initAll, since it will execute in place on that line.What Pekka says is correct, but I want to elaborate a little with an example that will help explain to someone who doesn't fully understand function pointers or delegates.
I won't use
window.onload
because that's a bit contrived to demonstrate. I'll use a simple multiply function to demo instead:This could equally be written:
While in the first example, the implication may not be obvious, the second example shows more clearly that we're assigning a function which has 2 parameters to a variable called
Multiply
, and this concept of functions as assignments is common throughout JavaScript. This is a small demonstration of the fact that functions are "first class citizens", that is, they can be passed around exactly as if we were passing around values.So now to the difference of assignment:
At the point of defining the ret variable,
Multiply
is executed and the return value is assigned -ret
becomes equal to 12.Let's try that again a different way:
Now, at the point of defining
ret
,ret
becomes yourMultiply
function as opposed to being the result obtained from yourMultiply
function. Calls toret()
will cause yourMultiply
function to be executed, and you can call it exactly as if you'd calledMultiply(operator, operand)
:is the same as
You have effectively said that you are going to use
ret
as a delegate forMultiply()
. When callingret
, we're really referring to theMultiply
function.Back to your
window.onload
. Think of this as:So as you can see,
window.onload
is a function just like any other function, there's nothing special about it. You can assign it a value, assign it a function, null it out if you wish - the point is that there's nothing any more special aboutwindow.onload
than there is about your own function. The only slightly different thing is that it gets called by the window when it's loaded. [Disclaimer: I've never actually nulled out window functions, so I'm not sure if this will cause negative repercussions. One would hope they check to see if a function is assigned before calling it i.e.if (window.onload) window.onload();
].Now calling
initAll()
what we're saying is:which might as well say:
But when we say
initAll
without the parentheses, what we're really saying is: I want to replace whatever my window.onload function is, with a new function - i.e. I want to replace it with myinitAll
function, so that any calls towindow.onload
runs myinitAll
code.So:
is replaced with:
So any call to
window.onload
will execute yourinitAll
function instead of whateverwindow.onload
was originally. You have replaced the original function with your new function.In fact, you could equally write:
Another example that may demonstrate better is this:
Whatever the time was at the time
d
is defined ends up assigned tocurrentTime
. Great, but that's only useful if we want to find out what time the function containing that code was called - i.e. at page load time. What if we want the current time any time thatcurrentTime
is called?Notice how we call
b()
in ourc
andd
assignments exactly as we could callcurrentTime()
?