Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 135 characters in body
Source Link
Denys Séguret
  • 384k
  • 90
  • 813
  • 780

That's because onload is already declared and null before your script executes.

This is similar to that code :

var v=null;
function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts null

which is different from this one :

function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts the function

When you declare a function like this, the declaration and assignment are logically hoisted to the "start" of the scope, so the assignment doesn't really occur after the onload function is given the null value.

That's why it's different from

window.onload=...

which isn't a declaration but only an assignment which can't be hoisted.

That's because onload is already declared and null before your script executes.

This is similar to that code :

var v=null;
function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts null

which is different from this one :

function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts the function

When you declare a function like this, the declaration and assignment are logically hoisted to the "start" of the scope, so the assignment doesn't really occur after the onload function is given the null value.

That's because onload is already declared and null before your script executes.

This is similar to that code :

var v=null;
function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts null

which is different from this one :

function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts the function

When you declare a function like this, the declaration and assignment are logically hoisted to the "start" of the scope, so the assignment doesn't really occur after the onload function is given the null value.

That's why it's different from

window.onload=...

which isn't a declaration but only an assignment which can't be hoisted.

Source Link
Denys Séguret
  • 384k
  • 90
  • 813
  • 780

That's because onload is already declared and null before your script executes.

This is similar to that code :

var v=null;
function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts null

which is different from this one :

function v(){
 console.log('hi');
}​​​​
console.log(v); // alerts the function

When you declare a function like this, the declaration and assignment are logically hoisted to the "start" of the scope, so the assignment doesn't really occur after the onload function is given the null value.

lang-js

AltStyle によって変換されたページ (->オリジナル) /