reflection: variable -> string (not string -> variable)

reflection: variable -> string (not string -> variable)

Post by erik.berno » Fri, 13 Feb 2009 22:37:48


is it possible to do something like the following?

{{{
var example = {};
for(var i = 0; i < 10; i++)
eval( nameof(example) + '_' + i + '= new Otherthing();');
}}}

if so, how to do it?
Yes in this example you just could write eval("example_" + i + "new
Otherthing"); But if this for-loop is written in a context where the
variable "example" can change to any other variable, you would have to
write infinite lines of code. I would like to find another
solution. ;-)

and just not to be seen in the wrong light:
1. yes I used a lot of googling before. There are many many pages who
describe how you can make a variable out of a string (with eval() of
course) but not so many for the direction I ask. And most times, the
asking person tries to solve another problem. So everybody starts
looking for another way of solution. That's not my intention. I really
want to know how to do _that_.
2. It may look as if I describe my guesses, not the symptoms (see
http://www.yqcomputer.com/ ~esr/faqs/smart-questions.html#symptoms). But it
really is my question, how I could do something like that.
3. impossible does not _look_ like the best solution. There must be
some kind of Reflection API. The JSON.stringify() must knows how the
variables are named.
 
 
 

reflection: variable -> string (not string -> variable)

Post by Gregor Kof » Fri, 13 Feb 2009 23:59:06


XXXX@XXXXX.COM meinte:

To do what? Depending on your nameof() function and Otherthing() this
will "work" without any problems.


No idea what you are talking about...


To do what? You haven't specified a problem. [1]
Apart from that: You *are* "making a variable" out of a string.

Given your subject line, I suppose you want to get the name of variable
into a string - but *why*? You can iterate through the global object and
find your global variables there - if that helps.

Gregor

[1]
http://www.yqcomputer.com/ #ps1Quest

 
 
 

reflection: variable -> string (not string -> variable)

Post by erik.berno » Sat, 14 Feb 2009 19:31:08


Hey Gregor,

thanks for you answer! You are right.

I wrote too much stuff in my mail that was not so clear. But as you
saw directly through my posting, the header tells exactly what I want.
Maybe you don't need to read or answer anything else.

And I have one question for you, too: For what do you need the "why"?
Your post looks exactly as if I answered it in my point 2 in the
appendix of my first mail. I know it looks like I didn't specify a
problem. Thats why I said I don't want to solve a problem. I want to
learn reflection in javascript. But especially on that part of the
reflection topic I could not find anything useful on the net. Will you
help me with that?
 
 
 

reflection: variable -> string (not string -> variable)

Post by Gregor Kof » Sat, 14 Feb 2009 19:58:37


XXXX@XXXXX.COM meinte:


It helps - frequently.


for ... in iterates through object properties. Typeof gives you
information about the property type. I guess that's it with reflection
in JS.

Gregor
 
 
 

reflection: variable -> string (not string -> variable)

Post by Jorg » Sat, 14 Feb 2009 20:09:12


Is that reflection or introspection ?

Jorge.