Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   compile a var name from another var (http://www.programmingforums.org/showthread.php?t=14157)

dpsleep Oct 11th, 2007 4:45 AM

compile a var name from another var
 
i have a script that holds the functions for a desklet. the desklet is to be ran in a program called avedesk. ultimately this block of code is meant to first check for the next side to flip to, then check if that side is enabled, then switch to it if it is, if its not, goto the next side and check it.

the variables of chk0-chk4 are ether true or false, true meaning the side is enabled false meaning it isn't and to skip it.

side is the current active side of the desklet

the newside stuff is just telling it if the current side is not the last side add one, else goto the first side.

then we got the while loop

now the problem im having is that im just starting js as this desklet program requires it. so i don't know much about the syntax and have been trying to get by on frequent Google searches.

the part im having trouble with is
:

if(newside == i && ("chk" + i) == 'true'){

what im trying to do is have the var i from the loop be added after chk so that if i == 2 it work read out to:
if(newside == i && ("ch" + i) == 'true'){
if(newside == i && chk2 == 'true'){

this is the best way i could come up with to get the reaction i want, i just don't know how to make the two add together.

:

function flipnext(){
        var chk0 = this.parameters('radarchk1');
        var chk1 = this.parameters('radarchk2');
        var chk2 = this.parameters('radarchk3');
        var chk3 = this.parameters('radarchk4');
        var chk4 = this.parameters('radarchk5');
        side = desklet.GetcurrentSide();
        if(side < 4){
                newside = side + 1
        }else{
                newside = 1
        }
        i = 0
        while(i < 5){
                if(newside == i && ("chk" + i) == 'true'){
                        desklet.FlipTo(i);
                        alert(i);
                }else if(("chk" + i) == 'false'){
                        newside + 1
                }
        i++
        }
}


sorry if this has been solved before, i didn't know what to call it so i didn't really know what to search for. any help on this method or maybe an alternative method would be greatly appreciated. thanks.

The Dark Oct 12th, 2007 8:03 PM

try
:

eval("chk" + i) == 'true'

dpsleep Oct 13th, 2007 3:53 AM

Quote:

Originally Posted by The Dark (Post 135103)
try
:

eval("chk" + i) == 'true'

thanks, works like a charm.

The Dark Oct 17th, 2007 7:37 PM

Re: compile a var name from another var
 
One of the posts that went missing in this thread (I can't remember who wrote it) pointed to this blog: Eval is Evil
I read through that and have made some modifications to some of my code that was using eval extensively to test for the existence of a list of variables. It now runs in 1/5th of the time! It was actually slowing the whole program down quite a bit, as this list has grown slowly from about 20 variables to over 500.

So thanks to whoever it was who posted it previously.

~s.o.s~ Oct 18th, 2007 10:37 AM

Re: compile a var name from another var
 
> So thanks to whoever it was who posted it previously.
Yeah no problems, I was as much surprised as you are regarding the missing post. ;-)

Just to make matters more clear, every variable you create is a property of some object or the another. In cases where you don't have explicit namespaces declared or trying to declare a function, the parent object is the 'window' object. It is easier to remember this if you keep in mind that almost everything in Javascript is a hash with key value pairs (even arrays!).

DaWei Oct 18th, 2007 3:07 PM

Re: compile a var name from another var
 
Quote:

...every variable you create is a property of some object or the another...
While this is true of some languages (not all), it is definitely not true of the underlying microprocessor, as a whole.

Therein lies the problem. Abstraction is a moving target. To define something as "evil", whether it be an eval or a goto, is to indulge in the kind of behavior that judges a food by the taste of the wrapper.

~s.o.s~ Oct 19th, 2007 10:52 AM

Re: compile a var name from another var
 
Quote:

Originally Posted by DaWei (Post 135253)
While this is true of some languages (not all), it is definitely not true of the underlying microprocessor, as a whole.

Yes I know, and this being 'Javascript forum', I was specifically referring to browser embedded javascript.

Quote:

Therein lies the problem. Abstraction is a moving target. To define something as "evil", whether it be an eval or a goto, is to indulge in the kind of behavior that judges a food by the taste of the wrapper.
Judging the content without reading the context is to indulge in the kind of behavior that judges a food by the taste of the wrapper. The 'evil' there is used to emphasize how frequently eval is misused and misunderstood by programmers.


All times are GMT -5. The time now is 12:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC