0

Lets assume modulex and moduley both are not declared at all.
Now consider two scenarios:

var modulex = modulesx || {}; // This line of code works fine

moduley = moduley || {}; // but this code throws error saying moduley is undefined.

Again,

var modulex = moduley || {}; // This also throws error saying moduley is undefined.


Please elaborate on this one.

Abhijeet Pawar
  • 690
  • 6
  • 19

3 Answers3

1

You can't use variable which was not declared on the left side of "=" or as function parameter. Reference is not initialized.

Damask
  • 1,754
  • 1
  • 13
  • 24
1

In JavaScript, variable declarations are hoisted. This code:

var a = a || {};

Is actually interpreted like this:

var a;
a = a || {};

In your second example, moduley just isn't defined, which is exactly what your error says.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • Then var a = a || {} doesnt make any sense.. because it would be as good as saying var a= {} ; Also even if a is undefined it should have been interpreted as false and should have taken the value after || operator... Isn't it? – Abhijeet Pawar Jan 17 '13 at 07:18
  • 1
    @AbhijeetPawar: Take a look at this: http://stackoverflow.com/questions/833661/what-is-the-difference-in-javascript-between-undefined-and-not-defined – Blender Jan 17 '13 at 07:21
  • Adding more to question... if I "declare" the same variable twice then the second declaration will have no effect at all... am I right? – Abhijeet Pawar Jan 17 '13 at 07:51
  • @AbhijeetPawar: Try it out. It doesn't do anything. – Blender Jan 17 '13 at 07:52
0

Since your 'moduley' was not defined before using it, javascript compiler will call it as 'undefined'

gurvinder372
  • 66,980
  • 10
  • 72
  • 94