I do not understand the || and {} at the end of the first line here. Please could you explain.
var BubbleShoot = window.BubbleShoot || {};
BubbleShoot.Game = (function($){
var Game = function(){
var curBubble;
I do not understand the || and {} at the end of the first line here. Please could you explain.
var BubbleShoot = window.BubbleShoot || {};
BubbleShoot.Game = (function($){
var Game = function(){
var curBubble;
||
is the "or" operator in JavaScript. It returns the value on the left if the value on the left is truthy, otherwise it returns the value on the right.
undefined
and null
, are both falsey values in JavaScript, so if window.BubbleShoot
is one of these, the first line of your code will set the value of BubbleShoot
to be {}
, which is an empty JavaScript object on which you can set properties like Game
as shown in your second line of code.
The || is the JavaScript operator for 'OR', and the {} is defining a new, empty hash;
Essentially, it's equivalent to the following:
if (window.BubbleShoot != null && window.BubbleShoot != undefined) {
BubbleShoot = window.BubbleShoot;
} else {
BubbleShoot = {}; // new, empty hash
}