Javascript Variable Declaration with || [duplicate] - javascript

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.

Related

Why isn't this variable raising when a function is executed? [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)
(5 answers)
Closed 11 months ago.
So I'm making an idle game and I want when you buy a 'carrot patch' in this example, the price of said 'carrot patch' will raise by 50%. However this doesn't happen.
Here's my code:
var patchPrice = 10;
function buyPatch() {
if (affordable = true) {
var patchPrice = patchPrice * 1.5;
}
}
you have declared the patchPrice variable twice. so it will overwrite the previous value. just remove the second var and it will work.
These were problems with js before es6 I recommend you to use es6 const and let to declare variables.
Try == in the comparison or simply omit the (== true) as it is already a boolean.
var patchPrice = 10;
function buyPatch() {
if (affordable == true) {
var patchPrice = patchPrice * 1.5;
}
}
Avoid using "VAR" unless you wanna work with function scope variables.

JavaScript: How to change property of variables stored in an array [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Using Variable for Property Name of Object - Javascript [duplicate]
(2 answers)
Closed 4 years ago.
I have an array that contains variables that are used to control a template.
divisionsListToHide: ['showActivitiesList',
'showAssignActionplanDiv',
'showPropertiesListDiv',
'showAddActivityDiv',
'showCurrentActivity',
'editCurrentActivity'
],
I want to call a method that will set all variables contained in the divisionsListToHide array to false, i.e.
this.showAssignActionplanDiv = false;
this.showPropertiesListDiv= false;
I am trying to create a method (function) to accomplish this:
hideDivisions: function()
{
for(var i = 0;i<this.divisionsListToHide.length;i++)
{
var xx = 'this.' + this.divisionsListToHide[i]; // e.g. this.showActivitiesList
console.log(xx);
eval(xx) = false;
}
},
I was hoping to accomplish this through eval(), however, it displays "Uncaught ReferenceError: Invalid left-hand side in assignment". Any suggestion, how to accomplish this?
this is an object, and you can access its properties in two ways:
this.propertyName and this["propertyName"]. These two ways are the same, except the second one can be used with a variable just like in your case:
var prop = "propertyName";
this[prop] = false; // is equal to this["propertyName] = false, which is equal to this.propertyName = false
So try this:
hideDivisions: function()
{
for (var i = 0; I <this.divisionsListToHide.length; i++)
{
this[this.divisionsListToHide[i]] = false;
}
}

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
}

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.

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