Build an HTML5 game - No Starch [duplicate] - javascript

This question already has answers here:
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
Closed 7 years ago.
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
}

Related

what does it mean !! (not not) in javascript and when to use it [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 5 years ago.
i try to reading code of pandajs game engine and according to this code from github :
playMusic: function(name, loop) {
var volume = this.musicMuted ? 0 : this.musicVolume;
if (typeof loop === 'undefined') loop = true;
if (this.currentMusic) this._stop(this.currentMusic);
this.currentMusic = this._play(name, !!loop, volume);
this.currentMusicName = name;
return !!this.currentMusic;
}
and it's return !!this.currentMusic i try to figure it out but i cant understand why he use it. for example this code :
function a(arg){
console.log(!!arg);
}
a(true)
a(false)
if i pass true it's print true and if i pass false it's print false , so why not just return this.currentMusic instead of !!this.currentMusic or in my example just console.log(arg) instead of console.log(!!arg)
it coerces the object to boolean, if it was null, undefined etc it would be false else true
let x = null;
let y = 1;
console.log(!!x);
console.log(!!y);

How to find out the variable type in JavaScript? [duplicate]

This question already has answers here:
Finding Variable Type in JavaScript
(12 answers)
Closed 6 years ago.
I'm new to JavaScript and is writing a simple website using Netbeans. Since JavaScript is dynamically typed language, I was wondering how can I find out the type of a variable in situations where I am unsure of.
For example, how can I find out the variable type of emailAddress or domainPart in the code below?
function getEmailAndDomainParts(){
var emailAddress = document.getElementById("EmailAddress").value;
var emailPart = emailAddress.substring(0, emailAddress.indexOf("#"));
var domainPart = emailAddress.substring(emailAddress.indexOf("#") + 1);
document.getElementById("Email").value = emailPart;
document.getElementById("Domain").value = domainPart;
}
// test data
var myArray = ['a', 'b', 'c'];
// the usual typeof isn't very useful
alert(typeof myArray);
// this instance of method tests the array
// to see if it is an instance of the 'Array'
// constructor, which it is!
alert(myArray instanceof Array)
click here
you can use typeof:
The typeof operator returns a string indicating the type of the unevaluated operand
As pointed out in the comments, you cannot check the type of your variable but you can check the type of your variable's value using typeof():
x = "hello";
y = 123;
z = true;
console.log(typeof(x)); //Will return "string"
console.log(typeof(y)); //Will return "number"
console.log(typeof(z)); //Will return "boolean"

what does line mean: var DYN_WEB = DYN_WEB || {}; in js? [duplicate]

This question already has answers here:
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
What does "variable = variable || {}" mean in JavaScript [duplicate]
(4 answers)
Closed 9 years ago.
var DYN_WEB = DYN_WEB || {};
I saw above code in one js file
Question:
what does this mean?
if DYN_WEB is not null it will take the value already set for DYN_WEB else assign an empty object to it.
This is shorthand for
if ( ! DYN_WEB ) {
DYN_WEB = {}
}
or
var DYN_WEB = DYN_WEB ? DYN_WEB : {}
it means if the variable DYN_WEB has a value which is evaluated to binary true, then keep that value, otherewise assign {} to it. The latter will happen if the previous value of the variable is 'falsy' ie. one of the false, null, undefined, NaN, 0, "", [] or {} and in case it's not defined.

Javascript Variable Declaration with || [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 8 years ago.
I'm investigating Three.js at the moment and have come across this variable declaration at the top of the main source file:
var THREE = THREE || { REVISION: '52' };
I'm just wondering what the OR (||) is doing in there - what is the function of it?
The above means:
If the value of THREE evaluates to true, assign the value of THREE to the THREE variable, otherwise initialize it to the object { REVISION: '52' }.
In code, it's like saying:
var THREE;
if (THREE) {
THREE = { REVISION: '52' };
}
else {
THREE = THREE;
}
Or:
var THREE = (THREE) ? { REVISION: '52' } : THREE;
lazy instantiation. If the variable is declared already, then assign a value to it.

What does "options = options || {}" mean in Javascript? [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 7 years ago.
I came over a snippet of code the other day that I got curious about, but I'm not really sure what it actually does;
options = options || {};
My thought so far; sets variable options to value options if exists, if not, set to empty object.
Yes/no?
This is useful to setting default values to function arguments, e.g.:
function test (options) {
options = options || {};
}
If you call test without arguments, options will be initialized with an empty object.
The Logical OR || operator will return its second operand if the first one is falsy.
Falsy values are: 0, null, undefined, the empty string (""), NaN, and of course false.
ES6 UPDATE: Now, we have real default parameter values in the language since ES6.
function test (options = {}) {
//...
}
If you call the function with no arguments, or if it's called explicitly with the value undefined, the options argument will take the default value. Unlike the || operator example, other falsy values will not cause the use of the default value.
It's the default-pattern..
What you have in your snippet is the most common way to implement the default-pattern, it will return the value of the first operand that yields a true value when converted to boolean.
var some_data = undefined;
var some_obj_1 = undefined;
var some_obj_2 = {foo: 123};
var str = some_data || "default";
var obj = some_obj1 || some_obj2 || {};
/* str == "default", obj == {foo: 123} */
the above is basically equivalent to doing the following more verbose alternative
var str = undefined;
var obj = undefined;
if (some_data) str = some_data;
else str = "default";
if (some_obj1) obj = some_obj1;
else if (some_obj2) obj = some_obj2;
else obj = {};
examples of values yield by the logical OR operator:
1 || 3 -> 1
0 || 3 -> 3
undefined || 3 -> 3
NaN || 3 -> 3
"" || "default" -> "default"
undefined || undefined -> undefined
false || true -> true
true || false -> true
null || "test" -> "test"
undefined || {} -> {}
{} || true -> {}
null || false || {} -> {}
0 || "!!" || 9 -> "!!"
As you can see, if no match is found the value of the last operand is yield.
When is this useful?
There are several cases, though the most popular one is to set the default value of function arguments, as in the below:
function do_something (some_value) {
some_value = some_value || "hello world";
console.log ("saying: " + some_value);
}
...
do_something ("how ya doin'?");
do_something ();
saying: how ya doin'?
saying: hello world
Notes
This is notably one of the differences that javascript have compared to many other popular programming languages.
The operator || doesn't implicitly yield a boolean value but it keeps the operand types and yield the first one that will evaluate to true in a boolean expression.
Many programmers coming from languages where this isn't the case (C, C++, PHP, Python, etc, etc) find this rather confusing at first, and of course there is always the opposite; people coming from javascript (perl, etc) wonders why this feature isn't implemented elsewhere.
Yes. The sample is equivalent to this:
if (options) {
options = options;
} else {
options = {};
}
The OR operator (||) will short-circuit and return the first truthy value.
Yes, that's exactly what it does.
Found another variation of this:
options || (options = {});
Seems to do the same trick.

Categories

Resources