0

why we can do this,

var a = 1,
    b = 2,
    c = a + b;

but we can't this in module pattern,

var example = example || {};
example.UI  = {
      a : 1,
      b : 2,
      c : a + b  // this is not possible unless I use "this" or full name example.UI.a/b
};

Why do we must have to use "this" or "full namespace" within example.UI to get sibiling properties...

It gets really difficult to use full name sapces...

Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • 1
    Your `c: a + b` cannot be made to work by introducing `this`, as the value of `this` has nothing to do with object initializer expressions. – Pointy Jun 28 '16 at 13:11
  • 1
    For the "why" part: procedurally assigning a bunch of variables and creating an object literal are indeed two different things. – deceze Jun 28 '16 at 13:11
  • @deceze how people overcome long namespace issue with module pattern then ? – Mathematics Jun 28 '16 at 13:13
  • Please see this answer: http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations – bpavlov Jun 28 '16 at 13:23

1 Answers1

1

because in your second example a b and c are not defined! what you've actually defined is example.UI.a example.UI.b and example.UI.c so in a way:

example.UI  = {
      a : 1,
      b : 2,
      c : 3
};

equals

example.UI.a=1,
example.UI.b=2,
example.UI.c=3,
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43