How do I make a global variable using local variables? [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I am making a new video game and I need help with a purchase feature.
I want to use local variables to "create" a global variable. For example:
function example(apple,banana) {
return unicorn.apple.banana.text
}
example(taco,burrito);
should return whatever unicorn.taco.burrito.text equals.

For this you need to use the Array Notation.
function example(apple, banana) {
return unicorn[apple][banana].text
}
example(taco, burrito);
Because you need to replace apple and banana, with their values.

Related

Define a function with a name in the string [duplicate]

This question already has answers here:
Dynamic function name in javascript?
(24 answers)
Closed 2 years ago.
I need to define a function with a name that I have in the string.
Not a big deal, right?
window[fnName] = function(...args) {
// ...
}
But what if I cannot access the window object? Like inside of Worker, for example.
Is it possible to do it without window and without eval()?
You can use self. It is property in both window and Worker scope that references back to their global scope. MDN/Window/self

My JavaScript function isn't using one of the variables i need it to [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I'm creating a mongoose schema and need to do required checks a lot, so I tried creating this function. But it says that 'subcategory' is initialized but never used. How can I get the function to check this. like I need it to?
function requiredCheck(subcategory, value) {
return this.subcategory === value;
}
Use bracket accessors to check a property of this by variable:
function requiredCheck(subcategory, value) {
return this[subcategory] === value;
}
Otherwise you're checking for the property with the literal key "subcategory" which probably isn't what you want.

Changing variable in quotes to variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
Sorry that my title isn't very clear - didn't really know what else to call this.
Basically what I want to do is have a variable that is
"authData."+service+".displayName";
And turn that into
authData.theValueGivenToService.displayName;
Any idea on how to do that?
(I'm working with JavaScript/JQuery by the way - I should have mentioned that earlier)
If you are trying to access a property of object then do:
displayName = authData[service].displayName;

How to complete a variable name with another variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 6 years ago.
I have an object that stores 3 variables called text0, text1 and text2 that in turn store one string each. I also have a variable that stores the index of the clicked button, so that if I click on the third button it should fetch the text2.
I was trying something like this:
p.text(myObj.text + btnNumber);
But so far all I get is NaN. Is it even possible to do this way? What am I missing?
Use named indexing of the object to get a variable named property:
p.text(myObj["text" + btnNumber]);
Javascript lets you index properties of an object as if it were a dictionary (indexed via string names).

Getting the value of an object's member through a variable [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
dynamically select a javascript object attribute
(1 answer)
Closed 8 years ago.
I have a javascript object (named resource) used for translating my texts.
resource
.fileNotFound >> "File not found"
.advSearch >> "Advanced search"
Usually I use this like below:
alert (resource.advSearch);
Now I need to access one of the member of this resource object through a variable.
Example:
var trans = "advSearch";
My question: how can I get the translation in my resource object for 'advSearch' contained in the trans variable?
Thanks.
You need to use the bracket notation instead of dot notation as the member operator
resource[trans]
You can use the [] notation to access properties as well.
resource[trans];

Categories