2

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.

Community
  • 1
  • 1
Craig Hooghiem
  • 1,272
  • 5
  • 15
  • 39
  • 3
    Can you explain why this is necessary? – mellamokb Jul 13 '12 at 15:32
  • 1
    `$var$bar = $foo;` isn't valid PHP. You *can* do `${"var$bar"} = $foo;`, though. – gen_Eric Jul 13 '12 at 15:32
  • you can achieve this using `eval()`, but it won't be easy to write all your code in eval :) – haynar Jul 13 '12 at 15:32
  • 1
    You can do that in PHP? :O If so it should've been on [this question](http://stackoverflow.com/q/1995113/419956). – Jeroen Jul 13 '12 at 15:33
  • If you know what you want is a hack, why are you going to do it, just makes you look foolish – Security Hound Jul 13 '12 at 15:33
  • 1
    So you have a pattern that you know to be evil, and you want to apply it to other languages. I don't know if it can be done in JavaScript but I sure hope it cannot. – laurent Jul 13 '12 at 15:33
  • AWOOGA AWOOGA, RED ALERT! I'm changing the bulb to RED. Step back and tell us what you're trying to achieve. There is a better way. Right now you think you want to do this, but soon you will know better. AWOOGA! – Phil H Jul 13 '12 at 15:34

2 Answers2

9

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).

var bar = 1;
var foo = "test";
var group = {};
group[bar] = foo;
console.log(group[1]);

Variable variables are, essentially, fake arrays/objects. Use real ones instead — they give you much more power and are much easier to maintain.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I got this to work. Ends up I was doing something totally wrong when binding to the upload button, so everything was working the first time I did it (before I tried my sinful ways), but I was just throwing everything at the first "file list". – Craig Hooghiem Jul 13 '12 at 16:06
1

Seeing your edit, I suggest you use an object to store your unique plupload instances.

var all_uploads = {};
all_uploads["unique_name_1"] = new plupload();
all_uploads["unique_name_2"] = new plupload();
all_uploads["unique_name_3"] = new plupload();

You can generate the "unique_name" strings in whatever way you want.

Another alternative is to use a simple array and push() each plupload instance on it. Unfortunately, you'll have to search the array every time you want to find a particular instance, but if the array will stay small the performance trade-off is pretty minimal.

kevin628
  • 3,458
  • 3
  • 30
  • 44
  • Ok, so this was my original approach, and the second one would never initialize. The buttons on the additional uploaders only functioned for the first one. – Craig Hooghiem Jul 13 '12 at 15:52
  • @gamerzfuse Without seeing more code (HTML and the Javascript that goes with it), it's hard to for us to diagnose and help your code's problems. – kevin628 Jul 13 '12 at 15:53