title: How to create instances of a global javascript namespace?
body: <p>I am a C++/Java developer that has been tasked with a javascript project and I cannot seem to figure this issue out.</p>
  
  <p>I am working with a javascript file that defines a namespace by direct assignment as described <a href="http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/" rel="nofollow">here</a></p>
  
  <p>So here is my attempt at an overly simplified example namespace</p>
  
  <pre><code>// testns.js
  var ns = { }
  ns.val = -1;
  
  ns.setVal = function(newVal) {
      this.val = newVal;
  }
  </code></pre>
  
  <p>And here is what I would like to do</p>
  
  <pre><code>//myScript.js
  function testNamespaceInstances()
  {
      var nsInstance1 = ns;
      nsInstance1.setVal(1);
  
      var nsInstance2 = ns;
      nsInstance2.setVal(2);
  
      console.log("nsInstance1.val: " + nsInstance1.val);
      console.log("nsInstance2.val: " + nsInstance2.val);
  }
  </code></pre>
  
  <p>That function will output</p>
  
  <pre><code>nsInstance1.val: 2
  nsInstance2.val: 2
  </code></pre>
  
  <p>Is there anything I can do without modifying the testns.js file that the namespace is declared in so that the function will output</p>
  
  <pre><code>nsInstance1.val: 1
  nsInstance2.val: 2
  </code></pre>
  
  <p>Edit: Added some more details to the example code.  I would also like to note I am attempting to simplify a more complex javascript file that I am working with</p>