I was trying to understand the point of var myObj = myObj || {};
and found this and this article explaining it. But I still don't see the situation in which I'd need it. Those articles seem to address the actual logic of it as its equivalence to a ? a : b
- I get that.
For example, I'm creating a HUD for my game,
game.HUD = game.HUD || {};
game.HUD.Container = me.Container.extend({
//...
Why can't I just create the object without using game.HUD = game.HUD || {};
?
If you know you need to initialize
game.HUD
for certain, then you don't need to use your snippet.If there's a possibility that
game.HUD
held a previous value that you don't want to overwrite, then that's when you would use your sample code.