Javascript creates a variable on its behalf
In PHP, we can do this:
$variable = "name_of_variable"; $this->{$variable} = "somevalue"; how to do it in javascript?
where the usage example should look like this:
function Apple(){ var name = "variable_name"; this.(name) = "value"; } console.log(new Apple()); for conclusion
[Apple: {variable_name:"value"}] to try:
this[name] = "value"; All objects can use dot and array notation to access a variable.
Also note that this will allow you to create pairs of name values ββthat are not available through dot notation:
var foo = {}; foo['bar-baz'] = 'fizzbuzz'; alert(foo.bar-baz); //this will not work because `-` is subtraction alert(foo['bar-baz']); //this will work fine If you create a new object literal, you can use string literals for value names with special characters:
var foo = {'bar-baz':'fizzbuzz'}; But you cannot use variables as key inside the object literal, because they are interpreted as the name to use:
var foo = 'fizz'; var bar = { foo:'buzz' } alert( bar.fizz ); //this will not work because `foo` was the key provided alert( bar.foo ); //alerts 'buzz' Since other responders mention eval , I will explain the case where eval can be useful.
Attention! Code using eval is evil ; proceed with caution.
If you need to use a variable with a dynamic name, and this variable does not exist on another object.
It is important to know that calling var foo in a global context binds a new variable to a global object (usually window ). However, in closure, the variable created by var foo exists only in the context of closure and is not tied to any particular object.
If you need the name of a dynamic variable in closure, it is better to use a container object:
var container = {}; container[foo] = 'bar'; Thus, if all that is specified, if the name of the dynamic variable is required and the container object cannot be used, eval can be used to create / access / change the name of the dynamic variable.
var evalString = ['var', variableName, '=', String.quote(variableValue), ';'].join(' ') eval( evalString ); You can use a square bracket in Javascript:
variable = "name_of_variable"; window[variable] = "somevalue"; You can do this with any object in Javascript.
var name = "var_name"; var obj = {}; obj[name] = 'value'; alert(obj.var_name); I suggest using associative arrays to do whatever you try to do, as they are much cleaner and easier to debug.
However, if you really insist, you can use eval() to accomplish this:
variable = "name_of_variable"; eval(variable + " = \"somevalue\""); // this will work, but please do not do it alert(name_of_variable); EDIT: He just drew my attention to the fact that it is much easier (and better) to do this simply by accessing the window object:
window[variable] = "somevalue"; window['name_of_variable'] = 'somevalue'; or
eval('var ' + variable_name + ' = ' + variable_name + ';'); Other than that, don't do this. Variable variables are never a good idea and make it almost impossible to debug problems when things (invariably) break.