-3

I have a global object APP which I use as a namespace for variables and functions in my applicaton:

var window.APP = {};

Does it make sense to "declare" variables in this namespace before assigning them?

var APP.myvar = null;
APP.myvar = new Cls();    

Or should I forget using var and just add properties when required to my APP object?

jl6
  • 6,110
  • 7
  • 35
  • 65

2 Answers2

2

APP.myvar is property of APP object. There is no need to initialize it in a way post proposes as by default it's value is equal to 'undefined'.

BUT if you want to initialize APP.myvar with 'null' value, do it in this way

APP.myvar = null
Egor Smirnov
  • 603
  • 4
  • 17
1

you don't need to declare a variable there. On the top of every JavaScript file just write

window.APP = window.APP || {}

that will declare it as an empty object if it is undefined, or leave it as window.APP if defined. Then you can just hang variables off of it as you please .

APP.yourVar = "something"
Josh
  • 119
  • 2