[Javascript] dynamic var names
Scott Reynen
scott at randomchaos.com
Tue Aug 7 19:33:07 CDT 2007
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
More information about the Javascript
mailing list