title: reusable javascript objects, prototypes and scope
body: <pre><code>MyGlobalObject;
  
  function TheFunctionICanUseRightAwaySingleForAllInstansesAndWithoutInstanse() {
      function() {
          alert('NO CONSTRUCTOR WAS CALLED');
      }
  };
  </code></pre>
  
  <p>The Long-named function must be callable from <code>MyGlobalObject</code>, which in turn must be available as a global (to <code>window</code>) variable in all times after script was loaded. It should support extensibility in accordance with latest standards.</p>
  
  <p>I'm at architectural dilemma of how to built JS base for an application (almost 100% JS).</p>
  
  <p>We need an object i.e. <code>window.MyObject</code> (like a module, like jQuery) so </p>
  
  <p>It can be created with</p>
  
  <p><strong>VAR1</strong></p>
  
  <pre><code> var MyGlobalObjConstructor = function(){
       this.GlobalFunctionInObject = function(){
          alert('called with MyGlobalObj.GlobalFunctionInObject()');
          }        
  };
  window.MyGlobalObj = new MyGlobalObjConstructor();    
  </code></pre>
  
  <p>Is <code>MyGlobalObj</code> extensible? Can I create child objects, which will inherit current state of <code>MyGlobalObj</code> (extended functions/properties <code>MyGlobalObj.NewFunc</code> e.g.)? What is the main difference between using prototype (VAR3)?</p>
  
  <p>By <code>GlobaldFunction</code> I mean single instance for all initialized/instantiated (possibly instantializable) instances..</p>
  
  <p>Or with</p>
  
  <p><strong>VAR2</strong></p>
  
  <pre><code>var MyGlobalObj = {
      GlobalFunctionInObject: function...
      GlobalFunctionInObject2: function...
  };
  MyGlobalObj.GlobalFunctionInObject();
  // here I lose all hierarchy elements, no prototype, 
  // can I use GlobalFunctionInObject2 in GlobalFunctionInObject?
  </code></pre>
  
  <p>Or with</p>
  
  <p><strong>VAR3</strong></p>
  
  <pre><code>var MyGlobalConstuctor = function(){} // already 'well-formed' object
  MyGlobalConstuctor.prototype.GlobalFunctionInObject = function...
  };
  var MyGlobalObj = new MyGlobalConstuctor();
  
  // so I'm sceptical to NEW, because I have ALREADY wrote my functions 
  // which I expect to be in memory, single instance of each of them, 
  // so creating MyObject2,3,4 with NEW MyGC() makes no sense to me.
  // DO I REALLY HAVE TO USE "MyGlobalConstuctor.prototype." FOR EACH FUNCTION?!!!!
  </code></pre>
  
  <p>What's the difference defining <code>MyGlobalObj</code> as a function and as an object (result of func or VAR2)?</p>
  
  <p><strong>OR VAR4?</strong></p>
  
  <p>I see in Chrome Debugger both prototype and <code>__proto__</code> special fields. I've read that that's OK, but why are they not saved in a single prototype?</p>
  
  <p>So, what is the correct/optimal way to implement <code>window.MyObject</code>, so one could <code>MyObject.MyFunction();</code> What are the differences (pro/contra) of variants 1 2 and 3?</p>