trying to understand some codes related to window.onload in js

Posted by user2507818 on Stack Overflow See other posts from Stack Overflow or by user2507818
Published on 2013-07-01T03:23:37Z Indexed on 2013/07/01 4:21 UTC
Read the original article Hit count: 102

Filed under:
<body>  
<script language="javascript">
window.tdiff = []; fred = function(a,b){return a-b;};
window.onload = function(e){ 
    console.log("window.onload", e, Date.now() ,window.tdiff, 
    (window.tdiff[1] = Date.now()) && window.tdiff.reduce(fred) ); 
}
</script> 
</body> 

Above code is taken from a site.

In firefox->console, it shows:

window.onload load 1372646227664 [undefined, 1372646227664] 1372646227664

Question:

  1. For window.tdiff->[undefined, 1372646227664], why not:[], because when runs to code:window.tdiff, it is still an empty array?

  2. For window.tdiff.reduce(fred)->1372646227664, window.tdiff = [undefined, 1372646227664], undefined - 1372646227664, should be NaN, why it shows 1372646227664?

© Stack Overflow or respective owner

Related posts about JavaScript