according to my knowledge, this feature already exists in PHP. lets look at the following php code:
$color = 'red';
$$color = 'dark';
description of the feature:
Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically.A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, red, can be used as the name of a variable.At this point two variables have been defined and stored in the PHP symbol tree: $color with contents "red" and $red with contents "dark".
my Question
can this be done in java Script?
-
1It might be convenient sometimes but it's a huge mass!ThiefMaster– ThiefMaster2012年01月15日 12:10:56 +00:00Commented Jan 15, 2012 at 12:10
2 Answers 2
Three different techniques come to mind, each with its warnings and (except the second one) uses:
1) You can declare a new variable in JavaScript anywhere in the code using the var keyword:
var $color = 'red';
The variable is actually defined throughout the scope in which the var occurs, even above the var statement — that is, these two functions are identical even though they look slightly different:
function foo() {
doSomething();
var x = 5;
x += doSomethingElse();
return x;
}
function foo() {
var x;
doSomething();
x = 5;
x += doSomethingElse();
return x;
}
This is because all vars take effect when the context for the function is created, not where they appear in the code. More: Poor, misunderstood var
2) If you just assign to a free symbol that's never been declared anywhere, you'll create an implicit global variable (not one constrained to the current scope), which is generally a bad idea. More: The Horror of Implicit Globals
3) Another thing you can do is have an object which is a container for various variables you want to track. You can create new properties on the object just by assigning them:
var data = {}; // A blank object
data.foo = "bar"; // Now `data` has a `foo` property
This technique is particularly handy when you need to track data that your script is completely unaware of, for instance based on user input, because you can use either dotted notation and a literal as above (data.foo), or you can use bracketed notation and a string (data["foo"]). In the latter case, the string can be the result of any expression, so all of these create a foo property on data:
// Dotted notation with a literal
data.foo = 42;
// Bracketed notation with a literal string
data["foo"] = 42;
// Bracketed notation with a string coming from a variable
s = "foo";
data[s] = 42;
// Bracketed notation with a string coming from an expression
s = "o";
data["f" + s + s] = 42;
5 Comments
s = "foo";, then using s as the name of a variable: data[s] = 42; means that data.foo has the value 42.window instead of data - that'll make them global.window[color]=document.getElementById("v") and I will use it several times in different places in my code. therefore the third way won't do it for me I suppose.AM I right?window is an object, like my data example. Assuming that color is a variable containing the string 'red', for instance, then window[color]=document.getElementById("v") will happily create a global variable red which refers to the element. Globals are properties of the window object, so color = 'red'; window[color] = 42; alert(window.red); /* alerts 42 */ alert(red); /* Also alerts 42 */ alert(window[color]); /* ALSO alerts 42 */ It's best to avoid global variables, but if you must use them, that will work.var color = 'red';
window[color] = 'dark';
console.log(color, red);
1 Comment
document.getElementById("v").innerHTML= red; it gives an error cant set the property of innerHTML of NULL