Equivalent of window["myString"] for Local Variable [duplicate] - javascript

This question already has answers here:
How can I access local scope dynamically in javascript?
(4 answers)
Closed 8 years ago.
Let say I got a string with the name of the variable I want to create.
If I want to create it with a global scope, I'd use window["myString"] = 100; or global["myString"] = 100; on Nodejs.
Is there a way to do the same but create the variable with local scope? (Ex: Inside a function)
EDIT: Note: The goal is to access the variable with its name directly. Ex: myString
I already know that I could easily create a object that would have the value as an attribute. Ex: obj.myString. But this is not what I'm looking for.

The local scope is not exposed as the global object is.
However...
function someFunction() {
var locals = {};
locals['someLocalVar'] = 'local var';
}

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

JS: local var X global (window) var [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
window.variableName
(6 answers)
Closed 4 years ago.
Studying global vars in JS here, I set out to try it and to my surprise this:
var thisVar = "global var";
function showVarLet() {
var thisVar = "local var";
console.log("%s %s", thisVar, window.thisVar);
}
showVarLet();
gives me:
local var
undefined
but the same in the browser console, gives me:
local var
global var
So, what´s with this window object?
EDIT:
I tried to check in the console what would happen if instead of window.thisVar I referenced this.thisVar, my assumption was that I would access the local variable but I keep accessing the global one, why so?
and the code I showed is in a function called global()
then none of the two thisVars is global, one is a local variable of the global() function, the other one is a local variable of showVarLet(). You cannot access local variables via window..

Creaating a javascript object with computed name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 6 years ago.
how would I make an object in javascript with a computed name.
Context:
I am making an add-on that will sit in the browser, log each hostname visited, create an object named after each one. I'm replacing "." with "_"
So for example on this site it would create a stackoverflow_com object.
is this possible?
another example would be
var 1+1 (with the variable actually being 2)
I know brackets make this possible with properies but I don't know how to do it with the name itself.
You need to store them either in a global object (like window) or preferably an object of your choosing
var mySites = {};
mySites["stackoverflow_com"] = "foo"; // access as mySites.stackoverflow_com or mySites["stackoverflow_com"]
mySites[1+1] = "bar"; // access as mySites[2];
More info: JavaScript property access: dot notation vs. brackets?

Create variable name based on argument sent to function in javascript [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I wanted to create variable name based on value sent to function in javascript.
like following, when i call function variable : variable("zebra"); this should return variable name as zebra1
function create variable(i){
return i+"1";
}
var variable("zebra")="abc";//this line should create variable zebra1 and initialise as abc
Try:
window['zebra'] = 'abc';
The window object holds all the global variables, assuming this is a request for global variables.
To specifically answer your question, you could put return window[i + '1'] = 'abc'; in your variable naming function.
Alternatively, you could create a global (or local) object named variables to hold all your variables:
function whoknows() {
var variables = {};
variables['zebra'] = 'abc';
}
Read more on working with objects at mozilla.org
You can create global variable with
window["zebra"] = "abc";
and use later ether with the same indexer syntax or directly - zebra.

Convert String name into javascript variable name? [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 8 years ago.
Problem with example:
Variable name: var product5519 = 10;
I can get this name in the form of a String i.e
var str = "product5519"
Is there any way to convert it into variable name so that i can use the value assigned to
product5519
I know one way to solve this problem i.e using eval(str)
If there is any another way to solve it please tell?
Once you are certain creating a global variable was the Right Thing to do, you can add your variable to the window object, like so:
window[str] = 42;
This works because variable lookups end up trying the window object if the variable was not defined in an inner scope.
It's a bit hacky but if you wanted to make a global variable you could do:
var str = "product5519";
window[str] = value;
You could then access the variable like:
window[str];
window.str;
str; // Assuming that there is no local variable already named "str"
you could do something like:
window['product5519'] = 'value'
it may be better to have an array of products, depending on the situation ofc.
You can use an associative array:
var variables = [];
variables['product5519'] = 10;
console.log(variables['product5519']);

Categories