Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:18:17+00:00 2026-06-01T15:18:17+00:00

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>

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:18:19+00:00Added an answer on June 1, 2026 at 3:18 pm

    You are expecting it to behave like languages you are used to with block scope. I suggest reading through Douglas Crockford’s book “The Good Parts” to get a better understanding of why everything works the way it does. If you learn all of this from the start you will have a much easier time down the road.

    Javascript is functionally scoped.. in this example test is scoped inside of foo.

    var foo = function() {
        var test = "Peanut Butter";
    };
    

    In this example, you can see that the function can modify test as it’s a globally scoped variable.

    var test = "peanut butter";
    var foo = function() {
        test = "Apple sauce";
    };
    

    There are three ways to define globally scoped variables, all of which I would suggest you avoid (not entirely, that’s impossible). Global variables are necessary, however they can be mitigated. Their lifetime is as long as the javascript is loaded. If you define multiple global variables in different .js files that are loaded by the same page, those global variables are accessible by both, making it quite easy to accidentally overwrite vars from different files.

    1: Place a var statement outside of any function, like you have done.

    2: Add it as a property to the global object.

    window.foo = value;
    

    3. use a variable without declaring it, this is an implied global. Really watch out for these. The function you declared is actually an implied global itself called “clickhandler”.

    foo = value;
    

    A common practice to minimize the use of global variables is known as Global Abatement, in which you define an application specific global variable to hold the rest of your global variables, to avoid collision with other libraries. This is done by declaring an object literal and using the 2nd method of creating global variables. Only use this when you need global variables however, and try to stick to functional scope when possible.

    var AWESOMENEWAPP = {};
    AWESOMENEWAPP.test = "Peanut Butter";
    

    When you get deeper in to the world of javascript, you’ll start learning about useful things like closures and more to keep your code clean and organized.

    Just don’t expect javascript to work like your typical classical languages and you’ll find that it is a powerful language in its own right, just different.

    On your journey, I suggest using the tool JSLint to test your code for following conventions and errors you won’t see because it isn’t compiled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner here See - http://www.w3schools.com/js/tryit.asp?filename=try_dom_tablerow_cells In this example, when change content is pressed, new
Absolute beginner question here. I have two lists in mathematica. The first one was
Beginner's question here: If I'm writing an If statement that I want to pertain
A beginner question, bear with me: I'm just wondering under what circumstances one should
Beginner question here. I'm going to make a Jquery function that is used to
Beginner here! Forgive me in advance for raising what is probably an incredibly simple
Beginner here: In this answer to my question of how to insert data into
Beginner here, I have a simple question. In Android what would be the best
beginner question. here's pdb output (Pdb) entry.image <FieldFile: None> (Pdb) entry.image is None False
Beginner rails question: How does one return a file from a controller in rails?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.