[Javascript] dynamic var names

David Dorward david at dorward.me.uk
Tue Aug 7 06:39:22 CDT 2007


On 7 Aug 2007, at 10:12, Michael Borchers wrote:
> I dynamically create a name for a var, let's say
> var myDynVar    = 'foo';
> Now I want to fill (var) foo with a value f.e. 'bar', so the next  
> time I
> alert(foo)    results in => bar;

Create an object. Use the myObject[myDunVar] property. (While you  
could use the window object directly - don't, its dirty, unintuitive  
and painful to maintain).

> I tried several combinations with eval()

Keep away from eval(), its almost always the wrong solution to any  
problem.

-- 
David Dorward
http://dorward.me.uk/
http://blog.dorward.me.uk/





More information about the Javascript mailing list