Is it simply about executing multiple functions on the window.onload
event?
Only one function will be executed on the window.onload
event.
Here's why:
There are two important operators in play here: =
and ,
:
window.onload = function1(){ }, function2(){ }()
However, since the =
operator takes precedence over the ,
operator, the left side of the ,
becomes this:
window.onload = function1(){ ... }
instead of this:
function1(){ ... }
This is why function1
gets assigned to window.onload
and not function2
.
Then the right side of the ,
operator runs, meaning function2
- as an IIFE - gets invoked there on the spot, but its return value isn't stored anywhere.
For details on precedence rules, check out: operator associativity & precedence.
To summarize the order of execution:
- function1 is passed (not executed) to window.onload
- function2 gets executed
- ... at some point later when the document finishes loading, function1 executes