1

So I am looking over a project which includes the following line of javascript:

window.negotiationApp = window.negotiationApp || {};

Can anyone explain might be going on with this line of code?

Update So now that I understand what this line of code is doing, my question maybe unique in asking the following:

There is no negotiationApp object in the javascript code. window.negotiationApp will always be set to an empty object, it seems the developer is really just using this as a namespace or container for other objects. Is this a common javascript practice?

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • Thanks for the quick answers guys, it looks like the developer that wrote this code never creates a negotiationApp object and so basically its usage seems to be that of a namespace in his code. – Brian Ogden Jun 09 '13 at 18:44
  • The same as `window.negotiationApp || (window.negotiationApp = {})` :) – Paul S. Jun 09 '13 at 18:45
  • More duplicates: http://stackoverflow.com/q/13268848/218196, http://stackoverflow.com/q/4087543/218196, http://stackoverflow.com/q/13016235/218196, http://stackoverflow.com/q/2802055/218196, http://stackoverflow.com/q/9604865/218196 -- Looks like we have to clean this up a bit. – Felix Kling Jun 09 '13 at 18:46
  • @FelixKling, I updated my question as there may be one aspect of my question that makes this unique, if not I will be happy to delete the question if this is a stackoverflow best practice, but I don't want people that took the time to properly answer this question to lose their points... – Brian Ogden Jun 09 '13 at 21:41
  • 1
    I'd just leave it be, Brian. No harm in a few more quality answers. – Michael Petrotta Jun 10 '13 at 02:11

4 Answers4

5

it makes sure that window.negotiationApp is set. If window does not have this property then it initializes it to be {} (an empty object), otherwise, it does nothing.

akonsu
  • 28,824
  • 33
  • 119
  • 194
  • Thanks for your answer, the developer that wrote this code does not ever create an negotiationApp object so it is always initialized as an empty object ({}), is this a common practice to achieve a namespace in javascript? – Brian Ogden Jun 09 '13 at 18:47
  • I do not know about how common this is, but most of the code that I have seen uses objects like this to put all the code's functionality in. – akonsu Jun 09 '13 at 18:50
2

It's an idiom that basically means, if window.negotiationApp doesn't exist, set it to {}. You might do that so future info doesn't return undefined or something.

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
2

Ensures window.negotiationApp object is not undefined.

window.negotiationApp = window.negotiationApp || {};

Means if window.negotiationApp is defined then use it or assign window.negotiationApp an empty object.

if(window.negotiationApp) {
  window.negotiationApp = window.negotiationApp;
}
else {
  window.negotiationApp = {};
}
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
2

since this variable is set on the global scope, it makes sure not to override an existing one if there is any.

so it basically says, if there is already a negotiationApp variable defined - use it, if not create a new one.

Tomer
  • 17,787
  • 15
  • 78
  • 137