[Javascript] dynamic var names

Terry Riegel riegel at clearimageonline.com
Tue Aug 7 21:20:57 CDT 2007


Ok, I see what you are saying. Here is another one I have used it on.  
For the moment we will put aside security issues...

I have a variable called url ...

url="http://www.example.com?myvar=Joe

It is stored in a string and I slice and dice and get two other  
variables n and v like...

n="myvar"
v="Joe"

And now I want to do the following...

myvar="Joe"

I  can use indirection to do this assignment

@n=v

where @n is the variable myvar


Is there a better way to do this? I realize there are major security  
issues with this example.


Terry




On Aug 7, 2007, at 8:33 PM, Scott Reynen wrote:

> On Aug 7, 2007, at 4:59 PM, Terry Riegel wrote:
>
>> I have always called this indirection, and have found several uses  
>> for it. Not sure about js but I have used it on several server  
>> side projects. Here is a quick outline of one such usage.
>>
>> I had a user editable table that needed quite bit of processing  
>> for each row in the table. I needed a way to cache the results. So  
>> I created (via indirection) a variable whose name was the same as  
>> the ID from each row.
>>
>> So I needed a way to flag that a rows processing had been cached.  
>> I created a variable whose name was the row ID. Since this ID was  
>> assigned in the user editable table I had to use indirection to  
>> read/write to the variable.
>>
>> It is a little confusing, and Javascript may have better ways of  
>> doing this.
>
> I think most scripting languages use hashes, a.k.a. associate  
> arrays, to do something like this, where it's not really necessary  
> that a variable name be defined dynamically, only that some sort of  
> named reference to a value be defined dynamically.  A dynamic array  
> key, e.g. rowIsCached[rowId] = true, works just as well as a  
> dynamic variable name in this case, and removes the risk of naming  
> conflicts with other variables or reserved words.  I think  
> associative arrays and dynamic variable names are both specific  
> forms of indirection, a very general concept of using names/symbols  
> to refer to things indirectly.  Email addresses are another common  
> example of indirection.
>
> Peace,
> Scott
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>




More information about the Javascript mailing list