"cannot assign" workaround?

"cannot assign" workaround?

Post by Don Lancas » Wed, 30 May 2007 11:42:46

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.yqcomputer.com/ : XXXX@XXXXX.COM

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

"cannot assign" workaround?

Post by RobG » Wed, 30 May 2007 11:48:27

<URL: http://www.yqcomputer.com/ #FAQ4_39 >


"cannot assign" workaround?

Post by Randy Web » Wed, 30 May 2007 12:17:18

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

Stop mis-using eval.

window["eq" + ii] = //some calculated stuff

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

Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://www.yqcomputer.com/
Javascript Best Practices - http://www.yqcomputer.com/

"cannot assign" workaround?

Post by Dark » Wed, 30 May 2007 21:38:31

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
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();

"cannot assign" workaround?

Post by -Los » Thu, 31 May 2007 13:13:04

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://www.yqcomputer.com/ #FAQ4_40

...specifically states its appropriate usage.

Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.