title: How should I define a JavaScript 'namespace' to satisfy JSLint?
body: <p>I want to be able to package my JavaScript code into a 'namespace' to prevent name clashes with other libraries. Since the declaration of a namespace should be a simple piece of code I don't want to depend on any external libraries to provide me with this functionality. I've found various pieces of advice on how to do this simply but none seem to be free of errors when run through <a href="http://www.jslint.com" rel="noreferrer">JSLint</a> (using 'The Good Parts' options).</p>
  
  <p>As an example, I tried this from <a href="http://www.docstoc.com/docs/23966981/Advanced-JavaScript-New" rel="noreferrer">Advanced JavaScript</a> (section Namespaces without YUI):</p>
  
  <pre><code>"use strict";
  if (typeof(MyNamespace) === 'undefined') {
      MyNamespace = {};
  }
  </code></pre>
  
  <p>Running this through <a href="http://www.jslint.com" rel="noreferrer">JSLint</a> gives the following errors:</p>
  
  <pre><code>Problem at line 2 character 12: 'MyNamespace' is not defined.
  Problem at line 3 character 5: 'MyNamespace' is not defined.
  Implied global: MyNamespace 2,3
  </code></pre>
  
  <p>The 'Implied global' error can be fixed by explicitly declaring <code>MyNamespace</code>...</p>
  
  <pre><code>"use strict";
  if (typeof(MyNamespace) === 'undefined') {
      var MyNamespace = {};
  }
  </code></pre>
  
  <p>...and the other two errors can be fixed by declaring the variable outside the if block.</p>
  
  <pre><code>"use strict";
  var MyNamespace;
  if (typeof(MyNamespace) === 'undefined') {
      MyNamespace = {};
  }
  </code></pre>
  
  <p>So that works, but it seems to me that (since <code>MyNamespace</code> will always be undefined at the point it is checked?) it is equivalent to the much simpler:</p>
  
  <pre><code>"use strict";
  var MyNamespace = {};
  </code></pre>
  
  <p><a href="http://www.jslint.com" rel="noreferrer">JSLint</a> is content with this but I'm concerned that I've simplified the code to such an extent that it will no longer function correctly as a namespace. Is this final formulation sensible?</p>