"cannot assign" workaround?

D

Don Lancaster

I want to programically generate some JS variable names in a loop.

The variables are NOT to be array values.

The variable names might be eq0, eq1, eq2....

Inside an ii indexed loop,

eval ("eq" + ii ) = some calculated stuff ;

generates a "cannot assign" error.

So does

"eq" + 11 = some calculated stuff ;


What is the correct way to programatically generate variable names?


--
Many thanks,

Don Lancaster voice phone: (928)428-4073
Synergetics 3860 West First Street Box 809 Thatcher, AZ 85552
rss: http://www.tinaja.com/whtnu.xml email: (e-mail address removed)

Please visit my GURU's LAIR web site at http://www.tinaja.com
 
D

Darko

Don Lancaster said the following on 5/28/2007 10:42 PM:


I want to programically generate some JS variable names in a loop.
The variables are NOT to be array values.
The variable names might be eq0, eq1, eq2....
Inside an ii indexed loop,
eval ("eq" + ii ) = some calculated stuff ;
generates a "cannot assign" error.

Stop mis-using eval.

window["eq" + ii] = //some calculated stuff
"eq" + 11 = some calculated stuff ;
What is the correct way to programatically generate variable names?

Assign them as properties of the window, which they are anyway, using
bracket notation.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

OK, I know how it feels when someone you asked how to do something
tells you that you should do something else instead, though they might
be right at times, no point in not-answering your question too. Try
this:
eval( "eq" + ii + " = " + (some calculates stuff) + ";" );
that should work, though I didn't test it. The point is, you should
make the whole expression appear under eval();
 
L

-Lost

Darko said:
Don Lancaster said the following on 5/28/2007 10:42 PM:


I want to programically generate some JS variable names in a loop.
The variables are NOT to be array values.
The variable names might be eq0, eq1, eq2....
Inside an ii indexed loop,
eval ("eq" + ii ) = some calculated stuff ;
generates a "cannot assign" error.
Stop mis-using eval.

window["eq" + ii] = //some calculated stuff
So does
"eq" + 11 = some calculated stuff ;
What is the correct way to programatically generate variable names?
Assign them as properties of the window, which they are anyway, using
bracket notation.

OK, I know how it feels when someone you asked how to do something
tells you that you should do something else instead, though they might
be right at times, no point in not-answering your question too. Try
this:
eval( "eq" + ii + " = " + (some calculates stuff) + ";" );
that should work, though I didn't test it. The point is, you should
make the whole expression appear under eval();

You really should think outside of eval(). I realize you seem to be
intimately familiar with its use, but that might be telling a story
about the quality level of your code you may not wish everyone to realize.

If you are going to encourage its (mis)use, you should at least warn the
person you are replying to as to its improper use.

http://jibbering.com/faq/index.html#FAQ4_40

....specifically states its appropriate usage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top