title: Static variable in Javascript that is set only once
body: <p>I was tearing my hair out to get this done...particularly for an html5 detection script. I wanted a variable that is set only once and that can't be overwritten again. This is it:</p>
  
  <pre><code>var StaticConfiguration = {};
  StaticConfiguration.Main = {
      _html5: null
  }
  StaticConfiguration.getVariable = function(name) {
      return StaticConfiguration.Main["_" + name];
  }
  StaticConfiguration.setVariable = function(name, value) {
      if(StaticConfiguration.Main["_" + name] == null) {
          StaticConfiguration.Main["_" + name] = value;
      }
  }
  </code></pre>
  
  <p>First, I define a global object StaticConfiguration containing all of these variables - in my case, just "html5". I set it to null, since I want to set it inside the application. To do so, I call</p>
  
  <pre><code>StaticConfiguration.setVariable("html5", "true");
  </code></pre>
  
  <p>It's set then. If I try to set it again, it fails - of course, since _html5 is not null anymore. So I practically use the underscore to "hide" the static variable.</p>
  
  <p>This is helping me a lot. I hope it's a good approach - please tell me if not :)</p>