A beginner's question, probably a trivial one. Here's the XUL code snippet:
<window
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript" src="chrome://.../main.js"/>
<button label="Click me!" oncommand="clickhandler()"/>
</window>
The JavaScript code:
// main.js
var test = "Peanut butter"; // a global variable
function clickhandler() {
alert(test);
}
The interpreter processes the JavaScript file just after reading the main window's opening tag and then proceeds with the rest of XUL code. In my opinion, the test
variable should go out of scope in the very moment the interpreter finishes processing main.js
. Moreover, the clickhandler()
function should have gone out of scope too, which means, when the button is clicked, nothing should happen. Well, unless I declare them as document.test
and document.clickhandler()
, for instance. However, a simple experiment proved me wrong. Both the function and the variable exist when the button is clicked. What is the actual life span of variables declared like this? When are they destroyed? Are they around until the application exits? Any best practices and references are highly appreciated.