Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use dynamic variable names in JavaScript

In PHP you can do amazing/horrendous things like this:

$a = 1; $b = 2; $c = 3; $name = 'a'; echo $$name; // prints 1 

Is there any way of doing something like this with Javascript?

E.g. if I have a var name = 'the name of the variable'; can I get a reference to the variable with name name?

like image 231
Finbarr Avatar asked Feb 25 '11 12:02

Finbarr


People also ask

Can we use in variable name in JavaScript?

Variable names are pretty flexible as long as you follow a few rules: Start them with a letter, underscore _, or dollar sign $. After the first letter, you can use numbers, as well as letters, underscores, or dollar signs. Don't use any of JavaScript's reserved keywords.

How do you make a variable name dynamic in Java?

There are no dynamic variables in Java. Java variables have to be declared in the source code1. Depending on what you are trying to achieve, you should use an array, a List or a Map ; e.g. It is possible to use reflection to dynamically refer to variables that have been declared in the source code.

What is Dynamic JavaScript?

Dynamically-typed languages are those (like JavaScript) where the interpreter assigns variables a type at runtime based on the variable's value at the time.


2 Answers

Since ECMA-/Javascript is all about Objects and Contexts (which, are also somekind of Object), every variable is stored in a such called Variable- (or in case of a Function, Activation Object).

So if you create variables like this:

var a = 1,     b = 2,     c = 3; 

In the Global scope (= NO function context), you implicitly write those variables into the Global object (= window in a browser).

Those can get accessed by using the "dot" or "bracket" notation:

var name = window.a; 

or

var name = window['a']; 

This only works for the global object in this particular instance, because the Variable Object of the Global Object is the window object itself. Within the Context of a function, you don't have direct access to the Activation Object. For instance:

function foobar() {    this.a = 1;    this.b = 2;     var name = window['a']; // === undefined    alert(name);    name = this['a']; // === 1    alert(name); }  new foobar(); 

new creates a new instance of a self-defined object (context). Without new the scope of the function would be also global (=window). This example would alert undefined and 1 respectively. If we would replace this.a = 1; this.b = 2 with:

var a = 1,     b = 2; 

Both alert outputs would be undefined. In that scenario, the variables a and b would get stored in the Activation Object from foobar, which we cannot access (of course we could access those directly by calling a and b).

like image 156
jAndy Avatar answered Oct 19 '22 09:10

jAndy


eval is one option.

var a = 1; var name = 'a';  document.write(eval(name)); // 1 

Warning: Note that using the eval() function is not recommended if you don't know what you are doing, since it brings multiple security issues. Use something else unless absolutely necessary. See the MDN page for eval for more info.

like image 33
erickb Avatar answered Oct 19 '22 08:10

erickb