0

I cannot find the reason behind this format?

I assume it is to check if this variable has been created before and if so, to take that one rather than the blank object.

But I cannot think HOW or WHY this would already exist when I am assigning this.

Todd Vance
  • 4,627
  • 7
  • 46
  • 66
  • 2
    With namespacing, you might want to add into an existing namespace, rather than always create a new one. In your case, that may not be necessary, but that's why you might use it :) – c24w Apr 04 '13 at 17:04

2 Answers2

3

The notation performs exactly as you suggest.

This is really for cases where you might have multiple javascript includes which could potentially create the same object, and you do not want to clobber the object that might already be defined.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
2

You are right in your assumption that it will default to the existing variable if it exists, or otherwise create a new object.

Without seeing the code, it is a little difficult to tell how it could initialized previously, but it is typically used for modules/constructors that may already have been called. The purpose is to use a "singleton" rather than a new instance when that function is executed.

Igor
  • 33,276
  • 14
  • 79
  • 112