title: JavaScript NameSpace Global Variable vs Global Property
body: <p>I have two part question about creating namespaces in JavaScript, which one of the following is proper/better way to define a namespace:</p>
  
  <pre><code>// style 1
  var company = company || {};
  company.models = company.models || {};
  company.models.class = {...}
  
  // style 2
  company = typeof (company) == 'undefined' ? {} : company; 
  company.models = company.models || {};
  company.models.class = {...}
  </code></pre>
  
  <p>Please note there are TWO differences in the styles. First is use of typeof and Second is the style 2 does NOT use the var key. </p>
  
  <p>Both these styles seem to work and now here is where it gets really confusing. If I remove the var key from style 1 then namespace does NOT work at all. I get something like company is not defined. I think it has something to do with the var creating a global variable and without var we are creating a global property as explained in here</p>
  
  <p><a href="https://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it#answer-1471738">What is the purpose of the var keyword and when to use it (or omit it)?</a></p>
  
  <p>Would some please explain to me why the style 1 fails without the var key?</p>
  
  <p>Many thanks in advance. </p>