1

Here i am just creating a global namespace for my application and everything works as expected

var MYAPP = MYAPP || {} ; 

but if I omit var keyword ( i know it's not the right way), javascript throw an error "ReferenceError: MYAPP2 is not defined" .

MYAPP2 = MYAPP2 || {};

just out of curiosity can some one explain me in second case why javascript is not able to resolve the reference.

nitesh sharma
  • 591
  • 2
  • 5
  • 15
  • Maybe this helps: http://stackoverflow.com/questions/881515/javascript-namespace-declaration – Steve K Aug 13 '12 at 10:35
  • 2
    @StephanKristyn actually this is _exactly_ the way to implement namespaces in Javascript. See http://stackoverflow.com/a/6439636/6782 – Alnitak Aug 13 '12 at 10:35
  • 1
    in javscript OR operator, JavaScript will return first true value. the reason behind using it. correct me if i am wrong. – nitesh sharma Aug 13 '12 at 10:39

3 Answers3

2

The first version doesn't produce an error because Javascript's variable hoisting makes it equivalent to this:

var MYAPP;            // declares, but leaves the value undefined / unchanged
MYAPP = MYAPP || {};  // creates, if the value was previously undefined

Importantly, the declaration part of this does not overwrite any existing declaration or assignment that was already made to the variable.

This is what allows this pattern to be used repeatedly within a single scope. MYAPP either retains the value it already had, or is initialised to an empty object.

in the second case, that declaration step is effectively omitted. If the variable has not already been declared (or otherwise exists in scope, i.e. as a property of the global object) then the error you see is generated.

Alnitak
  • 313,276
  • 69
  • 379
  • 466
1

var MYAPP = MYAPP || {} declares variable MYAPP in current scope (aka execution context). If declaration appears in a function - local variable is declared; if it's in global scope - global variable is declared.

MYAPP2 = MYAPP2 || {}, on the other hand, is merely a property assignment. It first tries to resolve MYAPP2 against scope chain. If it finds it anywhere in that scope chain, it performs assignment; if it doesn't find MYAPP2, only then it creates x property on a global object (which is a top level object in a scope chain).

Shreedhar
  • 5,084
  • 3
  • 19
  • 27
  • All true - however I'm not sure this answers the OP's question re: what causes the error. The answer, as @Alnitak points out, has to do with hoisting. – Mitya Aug 13 '12 at 10:41
0

var keyword declare immediately variable you are referencing to

var a = a || {}; 

is like as

var a; a = a || {};

In second case you are refencing to variable isn't declared yet.

abuduba
  • 4,716
  • 7
  • 24
  • 40
  • Great... Should I delete an answer because you published a while I was writing not seeing any response ?:) – abuduba Aug 13 '12 at 10:48
  • There are people on SO who spot a good answer and basically duplicate it for cheap reputation points. There are other times when two people write at the same time. The former is regrettable, the latter annoying (for the slower typist) :) – Mitya Aug 13 '12 at 10:58