1

I'm following melon js tutorial. This is at the top of my HUD.js file.

game.HUD = game.HUD || {}

I've seen this before in other examples. What does the namespace.something = namespace.something || {} syntax mean and why is it needed?

Dan
  • 9,391
  • 5
  • 41
  • 73
user3871
  • 12,432
  • 33
  • 128
  • 268

1 Answers1

1

|| is a null coalesce operator so you are saying in that line return my object if it already exists or a new object if it does not (is null)

FGhilardi
  • 337
  • 1
  • 7
  • Ah, is this sort of the same thing as PHP's `$input = (isset($_GET['_input']) ? ($_GET['_input']) : 0);` ? – user3871 Mar 05 '14 at 22:48
  • Sort of. I'm not amazing at PHP so you might want to check with an expert but afaik PHP doesn't have a null coalescing operator. The `?:` operator you listed there is a ternary operator. How you used it performs a very similar function to the null coalesce. This link might help you? http://stackoverflow.com/questions/7278835/cs-null-coalescing-operator-in-php – FGhilardi Mar 06 '14 at 14:20