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.

7
  • 3
    Can you explain why this is necessary? Commented Jul 13, 2012 at 15:32
  • 1
    $var$bar = $foo; isn't valid PHP. You can do ${"var$bar"} = $foo;, though. Commented Jul 13, 2012 at 15:32
  • you can achieve this using eval(), but it won't be easy to write all your code in eval :) Commented Jul 13, 2012 at 15:32
  • 1
    You can do that in PHP? :O If so it should've been on this question. Commented Jul 13, 2012 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. Commented Jul 13, 2012 at 15:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

2 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.