I can't seem to find a question that answers this directly.
I'd like to use a function argument as the name of a variable within that function.
e.g.,
test(var1);
function test(foo)
{
var foo = 'Hello world!'; // Set var1
}
alert(var1); // Hello world!
Can I use the brackets here (i.e., window.[ ])?
obj[propNameExpression]. Then consider thatwindowevaluates to an Object and thus supports properties and normal property access (although some DOM/browser properties are "reserved"). That being said, usingwindow[prop], for an unconstrained prop value, is probably questionable. ConsidermyObject[prop]instead. You'll also need to pass in"var1"as a string value (or the identifier will be evaluated).