variable scope clarification
<p>I have a snippet of javascript code, that I have found in the "<strong>Secrets of the JavaScript Ninja</strong>" (a John Resig book). I have a problem to understand the behaviour of a variable. 
The following is the code (simplified with respect to the original one):</p>

<pre><code>(function() {
    var results;
    this.assert = function assert() {
        var li = document.createElement("li");
        results.appendChild(li);
        return li;
    };
    this.test = function test(name, fn) {
        results = document.getElementById("results");
        results = assert().appendChild(document.createElement("ul"));
        fn();
    };
})();
window.onload = function() {
    test("A test.", function() {
        assert();
        assert();
    });
};
</code></pre>

<p>My problem is the <strong>results</strong> variable. When you enter the "test" function, the results variable will take the value <strong>"ul#results"</strong> first and then the value <strong>"ul"</strong>, as a result of the <strong>appendChild</strong> function. But when you enter the <strong>"fn()"</strong> function the value of "results" is still "ul#results". Why? I have some difficult to understand the scope of this variable. </p>

<p>Can someone help me to understand this topic?</p>

<p>Thank you very much.</p>
