Re: Dynamic variable names



Second part: associative arrays.

In Javascript, being "object" oriented, you can access every element
via the dot notation. For instance knowing that a variable is named foo
var foo='hi';
it automatcially belongs to the window object.

Thus, stating:
window.foo
you are doing the same as accessing foo.

Yet, the dot notation is only another way to write what that thing is:
an Object. As such, it is a collection of PROPERTIES.
As everything in OOP which is a collection of properties, it can be
accessed as the KEY of an array (in javascript there is a much closer
relatuons between arrays and objects than in Java!):
window['foo']

Note that I have used quotes (apex). That is if you use the associative
power of the object and you resort to brackets [], the variable name
must be called in as a key, and as such can be either numerical or a
string, If a string, it should gon in between quotes. In fact, for an
object, its keys are all Strings (even NUMBERS are actually liuteral
numbers for a string, namely not say 1 but '1'.
Evidence of this:
var a=[1,2,3];
for(var i in a){alert(typeof i)}

aLl those alerts shall return STRING, because you loop it as an object.
Yet if you'd loop it as an array
for(var i=0; i<a.length;i++){alert((typeof i)+' '+(typeof a[i]))}
that will rerturn NUMBER NUMBER

I don't know of a better evidence that when you deal in an OO language
with a data structure treating it as an OBJECT, then ALL ITS KEYS
belong to the STRING data type.

Now and as a consequence of it, whatever is fit to return a STRING DATA
TYPE, is ALSO fit to be used as the KEY to unlock a property out of an
object. ALSO out of the window object, if a variable belongs to it.

Thus if you say
var foo='foo2';
foo2='hallo world';

if you now say
window[foo]
since foo holds a STRING, it triggers from the window scope the
PROPERTY whose name was the one HELD as a STRING by foo, namely foo2,
so
window[foo]
would return:
"foo2" which, grabbed then as a property of the window scope, would
grab the varbaile NAMED and initialized as foo2 and thus shall
eventually relinquis the VALUE held by the latter: "Hallo world".

In Php you'd have finer ways to do it using the double $$ notation.
Javascript uses other avenues, the one just seen.

Now, as long as you're working with javascript, which is what can be
assumed, you an quite safely refer to the window object.

So the answer object oriented to the question
=====
Inside the function, how do I assign a value to the variable named
myVarName
(i.e. the variable whose name is the value of strVarName)?
=====

is: use the ASSOCIATIVE POWER of an OO language like javascript, and
use square brackets.
If strVarName holds as a STRING the name of another variable, whatever
[strVarName]
notation is then fit to retrieve it as long as you know to which scope
the variable whose name is held as a String by strVarName belongs.
If you know in your application such variable belongs to the window
scope, then use window
window[strVarName]

Exploiting the asscoiative power of objects you can do any sort of
crazy things, which purists would disagree with and that yet you should
know you can do

<script>
function foo(){alert('hi')}

foo['aproperty']='hallo world';

foo();

alert(foo['aproperty'])

</script>

You are the owner of your own application so you should know where in
the Object Oriented hierarchy ladder your variables belong.
In javascript, and given the example you yourself provide, it is quite
normal to assume it fully safely belongs to the window scope - except
in the world of pointed ears of course, where he is busy to imagine all
the possible application environments which are less likely to be
yours, and where he finds plausible to say that the window scope is
unsafe in javascript (gee, LOL), and yet at the same time he presumes
it is exactly in a global scope that your variable resides (which,
actually, you did not specify at all. YOU alone can know your OWN
application!).

ciao and good luck
Alberto
http://www.unitedscripters.com/

.



Relevant Pages

  • Re: FAQ Sections - Feedback Wanted
    ... How do I do with a string? ... It could be a form control in a form, an attribute, a form element. ... | * Which newsgroups deal with javascript? ... | o How do I find the size of the window? ...
    (comp.lang.javascript)
  • Re: FAQ Topic - How do I close a window and why does it not work on the first one? (2009-09&
    ... A window opened by javascript can be closed without confirmation ... A window can be closed without confirmation using `windowRef.close' ... and I also do not appreciate its use of the ` character with whitespace ... Richard Cornford uses the - foo - style which I find confusing and hard to ...
    (comp.lang.javascript)
  • Re: FAQ Sections - Feedback Wanted
    ... those that one calls into existence with JavaScript ... or How do I convert a string to a number? ... | o How can I see in javascript if a web browser accepts cookies? ... | o How do I find the size of the window? ...
    (comp.lang.javascript)
  • Question about eval scope
    ... have a string of JavaScript that looks like this: ... I know it's not attached to window, ... excellent FAQ article regarding scope chains and closures, ...
    (comp.lang.javascript)
  • Returning selected items in an open folder
    ... items in an open folder window. ... Dim boo As Boolean ... '-- s1 is a unique string that's part of a specific open folder path. ... Private Sub Form_QueryUnload ...
    (microsoft.public.vb.general.discussion)