...

View Full Version : Syntax Question



peterinwa
06-05-2003, 05:29 PM
if (n==1){food1=temp}
if (n==2){food2=temp}

I need to expand the above by many times, so I am wondering if I can make it more efficient.

I tried the following but got a syntax error:

"food"+n=temp;

Thanks, Peter

liorean
06-05-2003, 05:32 PM
Why not
var
food=[];
food[n]=temp;?

peterinwa
06-05-2003, 05:36 PM
Can you say in English what var food=[]; is doing?

P.S.

It works perfectly! Thanks!!

liorean
06-05-2003, 05:42 PM
It does the same as var food=new Array; would. In other words, it makes food an array containing 0 elements.

peterinwa
06-05-2003, 05:44 PM
Thanks. I don't like to use code I don't understand.

kansel
06-05-2003, 05:44 PM
var food=[];
is assigning the new variable food an empty array. The side effect is that the variable food is cast as an array object.

It is a shorthand for this declaration
var food = new Array();

bah! damn my slow brain

RadarBob
06-06-2003, 11:21 PM
The side effect is that the variable food is cast as an array object.
Are you suggesting that this is a bad thing? If so why?

liorean
06-07-2003, 02:46 AM
Actually, it's not a side effect. It's a type declaration combined with a definition. It would only be a side effect if we were doing it on a previously existing object, which is not the case. But that's technicalities.

No, it's not a bad thing. It is in fact the very thing we're using to achieve what we want.

RadarBob
06-09-2003, 02:47 PM
Originally posted by liorean
Actually, it's not a side effect. It's a type declaration combined with a definition. It would only be a side effect if we were doing it on a previously existing object, which is not the case. But that's technicalities.

No, it's not a bad thing. It is in fact the very thing we're using to achieve what we want.

OK. Thanks. Just making sure I'm not missing something here.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum