Possible Duplicate:
Javascript Variable Variables
There’s a really handy thing in PHP (that I’m sure is sinful), but I’m looking for the JavaScript alternative.
In PHP I can do this:
$bar = 'var';
$$bar= 'test';
echo $var;
I am looking for something similiar in JavaScript.
I want to pass a name to a function and initialize a new variable with that name.
So:
function(name) {
var name = new Function();
}
Update:
Ok, here’s what I’m trying to do. I know there’s an easier way..
I want to use multiple instances of something (let’s say plupload for now).
var uploader = new plupload();
I am loading these dynamically and there will be multiples on a page.
The issue I’m having is that they all have to have a unique name, because I have to be able to call uploader.init(), uploader.refresh(), etc and have each one function independently.
As I said, I’m sure there’s a better way.. I’m just not privy to it.
It is sinful. In both PHP and JavaScript (and pretty much every other language) if you have a collection of related things, then you should represent them with an array (if they are ordered and not sparse) or an object/associative array (if they have names).
Variable variables are, essentially, fake arrays/objects. Use real ones instead — they give you much more power and are much easier to maintain.