referencing a javascript value before it is declared - can someone explain this
<p>I'm hoping someone can explain to me why the below javascript/HTML will show "door #2" when the html is viewed in a browser:</p>

<pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;script type="text/javascript"&gt;

        function testprint() {
            alert('door #1');
        };

        window.onload = testprint;

        function testprint() {
            alert('door #2');
        };

        testprint = function() {
            alert('door #3');
        };

    &lt;/script&gt;
    &lt;script type="text/javascript"&gt;

        function testprint() {
            alert('door #4');
        };
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Since only the declartion testprint occurs before window.onload is set to testprint, I would expect window.onload cause 'door #1' to show up.  In actuality, onload causes 'door #2'.  Note that it will do this whether the first declaration of test print is included or not.</p>

<p>The third and fourth declaration of testprint use different means of assigning the function, I tried this to see if it would override window.onload's behavior in the same was the second declaration of testprint does.  It did not.  Note that if I move the fourth declaration of testprint to the end of the first script block it would be called by window.onload.</p>
