what does line mean: var DYN_WEB = DYN_WEB || {}; in js? [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)
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.

Related

typeof options == 'object' && options, why does this return the object value? [duplicate]

This question already has answers here:
returning with &&
(3 answers)
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 3 years ago.
Why does the code below work?
Why does the object in the variable options get assigned to i_identify_as_boolean instead of a boolean?
I would expect a boolean value in this case. I would expect this to work if I would write typeof foo == 'object' ? foo : null but not in this way. In this way I would expect a boolean.
So why does it work? I've tried reading the typeof documentation and this but nothing illustrates this behavior.
let options = {hello: 'world'};
let i_identify_as_boolean = typeof options == 'object' && options;
let i_identify_as_object = typeof options == 'object' ? options : null;
console.log(i_identify_as_object.hello);
console.log(i_identify_as_boolean.hello);
If all conditions are truthy it will always set it as the last truthy variable.
let x = true && 5
x returns 5;
if you want to use the variable as a boolean value you can double negate it so that it returns a boolean
!!x

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);

Build an HTML5 game - No Starch [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)
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
}

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.

Define object by a variables string [duplicate]

This question already has answers here:
How do I make JavaScript Object using a variable String to define the class name?
(10 answers)
Using a variable value to call an array element
(7 answers)
Closed 8 years ago.
Hard to explain.. I basicly want to do the following:
var doWhat = "speak";
var speak = {
hello: function() { alert: "Hello!"; }
};
// Won't work
doWhat.hello();
It's a bad example, but you should be able to get what I mean.
Is it possible somehow ?
You can use eval(doWhat).hello();. That way the contents of doWhat will be evaluated to the object reference.
You can do something like
var doWhat = {}, str = "speak";
doWhat[str] = {
hello : function() {}
};
doWhat[str].hello();
jsName = 'PageIndexController';
//ST1
eval("if( typeof jsName === 'undefined')alert(111);");
//ST1
eval("if( typeof " + jsName + " === 'undefined')alert(222);");
//ST1 not work
//ST2 work and the output: 222;
//there are two different way using eval, we will get 2 different outcome.

Categories

Resources