javascript - Why does window.onload allow me to use functions when they are used as function expressions? -
this question has answer here:
i understand function expressions must expressed @ top if want use them vs function declarations hoisted top.
when call constructor window.onload, works. if don't have window.onload, , call constructor before function expression, code breaks.
<script type="text/javascript"> window.onload = function () { var c = new c(); } // works, if delete window.onload code breaks. var c = function () { console.log("test"); }; </script>
that's simple. window.onload executed later, when c defined, if call new c
throw because c undefined.
move call after definition, or define using 'function' keyword, not 'var'
Comments
Post a Comment