This is because onload already declared and null before the script is executed.
This is similar to this code:
var v=null; function v(){ console.log('hi'); }ββββ console.log(v);
which is different from this:
function v(){ console.log('hi'); }ββββ console.log(v);
When you declare such a function, the declaration and assignment logically rise to the "beginning" of the scope, so the assignment does not occur as soon as the onload function is null .
That's why it is different from
window.onload=...
which is not a declaration, but only an assignment that cannot be raised.
source share