Howto Get a Variable's Name (NOT VALUE) - Introspection????

E

EricGoogle

Hello All,

I am trying to figure out a how to get a variable's name from code.

Ex:

var MYVAR = 3;
alert( ????? );

OUTPUT: "MYVAR"

Searched under:
javascript introspection
and
"variable name" javascript

NOT FINDING ANY HELP ON THE WEB
- anyone know this?.. is it possible in the current spec of the
language???

Thanks - Eric
 
T

Thomas 'PointedEars' Lahn

EricGoogle said:
I am trying to figure out a how to get a variable's name from code.
Why?

Ex:

var MYVAR = 3;
alert( ????? );

OUTPUT: "MYVAR"

Searched under:
javascript introspection
and
"variable name" javascript

Strictly speaking, a variable in ECMAScript implementations has an
identifier, not a name.
NOT FINDING ANY HELP ON THE WEB

Your Shift key is malfunctioning.
[...] is it possible in the current spec of the language???

(Your Question Mark key doesn't work properly either.)

No, and it is not going to be possible in any implementation of a future
Specification. There simply is no 1:1 relationship between variables and
values as any variable may have any value.

But ISTM you meant something like

window.alert("MYVAR = " + MYVAR);

and you can write

function alertValue(s)
{
window.alert(s + " = " + eval(s));
}

// example
alertValue("MYVAR");

for a general solution.


PointedEars
 
J

John G Harris

Hello All,

I am trying to figure out a how to get a variable's name from code.

Ex:

var MYVAR = 3;
alert( ????? );

OUTPUT: "MYVAR"
<snip>

When you write var MYVAR you are telling the compiler two things.
First, you are telling it to create a variable, i.e to create space able
to hold a value. Second, you are telling it that when you write 'MYVAR'
you mean that variable.

You want to point to something and find out that the name you gave to it
was 'MYVAR'. But how do you point to it without saying 'MYVAR' ?

One way is to remember it separately :
var MYVAR = 3;
vars[14] = 'MYVAR';
Now you have to remember two things, 'vars' and 14, so you're worse off.

Perhaps you could get at the source code and search for the declaration.
Now you have to remember what to search for, so again you're worse off.

Perhaps there's a better way. What do you really want to achieve ?

John
 
V

VK

>Hello All,
I am trying to figure out a how to get a variable's name from code.

var MYVAR = 3;
alert( ????? );
OUTPUT: "MYVAR"

<snip>

When you write var MYVAR you are telling the compiler two things.
First, you are telling it to create a variable, i.e to create space able
to hold a value. Second, you are telling it that when you write 'MYVAR'
you mean that variable.

You want to point to something and find out that the name you gave to it
was 'MYVAR'. But how do you point to it without saying 'MYVAR' ?

One way is to remember it separately :
var MYVAR = 3;
vars[14] = 'MYVAR';
Now you have to remember two things, 'vars' and 14, so you're worse off.

Perhaps you could get at the source code and search for the declaration.
Now you have to remember what to search for, so again you're worse off.

Perhaps there's a better way. What do you really want to achieve ?

I am just curious about this nearly FAQ about getting the literal
values. Some people asking about it here are definitely not novices in
the programming, at least based on their language in problems'
description and code samples. So why always such a huge surprise "oh,
it is not possible?!" every time? Is there some largely used/learned
language where it is a trivia? What language is that and how does it
implement such mechanics for say multiple references?
 
E

Evertjan.

John G Harris wrote on 18 mei 2008 in comp.lang.javascript:
When you write var MYVAR you are telling the compiler two things.
First, you are telling it to create a variable, i.e to create space able
to hold a value. Second, you are telling it that when you write 'MYVAR'
you mean that variable.

No, not quite. and it depends on the implementation.

A holding space is created for an address pointer to a variable holding
space in memory.

This variable holding space can be changed by changing the pointer.

var a = 1;
a = "Blahblahblah'; // the pointer is changed to a string holding space.
 
B

Bart Van der Donck

Thomas said:
EricGoogle said:
var MYVAR = 3;
alert( ????? );
OUTPUT: "MYVAR"
[...]
is it possible in the current spec of the language???

No, and it is not going to be possible in any implementation
of a future Specification.

I'm not so sure about that. In Perl this phenomenon is known as
symbolic referencing, though for these cases it's absolutely advised
to use them only under (very) controlled conditions. Yet I found it
semantically one of the most beautiful programming constructs I've
ever seen:

$x = 'ab';
$ab = '123';
print $$x;

says '123'

$x = 'ab';
$ab = 'cd';
$cd = '456';
print $$$x;

says '456'

etc.

This same principle applies to dir/file symrefs under UNIX and
presumably exists in other languages too.

http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/43bb0d99c1c66833/
There simply is no 1:1 relationship between variables and
values as any variable may have any value.

At each Momentum in the code execution there is a 1:1 relation, I
think this is all that matters. If no variable name is found, then it
simply returns undef.
 
B

Bart Van der Donck

Evertjan. said:
John G Harris wrote on 18 mei 2008 in comp.lang.javascript:


No, not quite. and it depends on the implementation.

A holding space is created for an address pointer to a
variable holding space in memory.

This variable holding space can be changed by changing the
pointer.

In (very low level) mechanical terms, I would rather support Mr
Harris' statement.

VAR a INT (8)
...reserve 8 bytes in the available RAM for variable 'a'

a = "hi"
...populate 2 of the 8 bytes, 6 unused

a = "one"
...empty & repopulate to three (and not point to another
"physical" location in the machine)
 
E

Evertjan.

Bart Van der Donck wrote on 19 mei 2008 in comp.lang.javascript:
In (very low level) mechanical terms, I would rather support Mr
Harris' statement.

VAR a INT (8)
...reserve 8 bytes in the available RAM for variable 'a'

a = "hi"
...populate 2 of the 8 bytes, 6 unused

a = "one"
...empty & repopulate to three (and not point to another
"physical" location in the machine)

No Bart, it does not work that way with a supervariant variable,
and in JS all variables are like that. [super.. , because they also can
point to (= "be"/"become") an object].

The named variable has a pointer to [the location of ] the present
content, be it a string, a number, an object.

When the string is exchanged for a number, or for an object, or a longer
string space is necessary [perhaps even with any!! string manipulation],
only the pointer is changed, and if necessary space is allocated where
the pointer points to. Old value memory that is orphaned is recovered by
garbage collection later.

That's why string concatenation is so time intensive, but in general it
works very efficient.
 
H

Henry

In (very low level) mechanical terms, I would rather support Mr
Harris' statement.

VAR a INT (8)
...reserve 8 bytes in the available RAM for variable 'a'

a = "hi"
...populate 2 of the 8 bytes, 6 unused

a = "one"
...empty & repopulate to three (and not point to another
"physical" location in the machine)

You are not taking into account what is necessary for the system to
implement - typeof -. Suppose your 'reserved' 8 bytes where all zero
values, is that value stored a zero number, an empty string or a false
boolean value? You cannot work that out from the bytes themselves, and
so each value needs some additional information asserting its type.
And as soon as you need a value to be an association of the bytes of
data for the value and an assertion about its type it starts to make a
lot of sense for the 'values' of variables to be implemented as
'pointers' to structures elsewhere in memory.
 
B

Bart Van der Donck

Henry said:
You are not taking into account what is necessary for the
system to implement - typeof -.

Absolutely. I'm taking into account almost nothing; only the most
primitive mechanisms of declaration, assignment and memory allocation,
like a calculator without electricity could work.

It doesn't work like that in javascript for sure.
Suppose your 'reserved' 8 bytes where all zero values, is
that value stored a zero number, an empty string or a false
boolean value?

Doesn't matter; each byte just stores what it's told. It only depends
on the set what *becomes* the meaning of a byte. E.g. on 8-bit
systems, we could opt for the ASCII-set in order to agree that your:
- null = 00000000 (\x00)
- zero number = 01100000 (\x30)
- false = 00100110 (\x13) or 00101010 (\x15)
- ...

Of course this is a much too primitive presentation for javascript.
The level is much, much higher there with many levels in between.
You cannot work that out from the bytes themselves, and
so each value needs some additional information asserting
its type.

Before you can start working in a language like javascript:
definitely.
And as soon as you need a value to be an association of the
bytes of data for the value and an assertion about its type it
starts to make a lot of sense for the 'values' of variables to
be implemented as 'pointers' to structures elsewhere in memory.

Yes, this concept has proven to be more effective once computer power
reached a certain level.
 
T

Thomas 'PointedEars' Lahn

Bart said:
Thomas said:
EricGoogle said:
var MYVAR = 3;
alert( ????? );
OUTPUT: "MYVAR"
[...]
is it possible in the current spec of the language???
No, and it is not going to be possible in any implementation
of a future Specification.

I'm not so sure about that. In Perl this phenomenon is known as
symbolic referencing, though for these cases it's absolutely advised
to use them only under (very) controlled conditions. Yet I found it
semantically one of the most beautiful programming constructs I've
ever seen:

$x = 'ab';
$ab = '123';
print $$x;

says '123'
[...]

What you are describing is also possible in ECMAScript implementations, with
the bracket property accessor:

// in global context
var x = "ab";
var ab = "123";

// displays "123"
window.alert(this[x]);

When `ab' is the name of the property of an object different from the Global
Object, this works as well if you replace `this' (or the reference to the
Global Object) with a reference to the owning object.

However, neither is what I understood the OP to be asking about.
At each Momentum in the code execution there is a 1:1 relation,

Huh? No, most certainly there isn't:

var x = 42;
var y = 42;
this["z"] = x;
getMe(42)
I think this is all that matters. If no variable name is found, then it
simply returns undef.

What do you want getMe() to return in this example, then?


PointedEars
 
B

Bart Van der Donck

Thomas said:
Bart said:
$x = 'ab';
$ab = '123';
print $$x;
says '123'

What you are describing is also possible in ECMAScript
implementations, with the bracket property accessor:

// in global context
var x = "ab";
var ab = "123";

// displays "123"
window.alert(this[x]);

When `ab' is the name of the property of an object different
from the Global Object, this works as well if you replace
`this' (or the reference to the Global Object) with a reference
to the owning object.

However, neither is what I understood the OP to be asking about.
At each Momentum in the code execution there is a 1:1 relation,

Huh? No, most certainly there isn't:

var x = 42;
var y = 42;
this["z"] = x;
getMe(42)
I think this is all that matters. If no variable name is
found, then it simply returns undef.

What do you want getMe() to return in this example, then?

Yes. The relation is 1-to-N, sorry. Trying to retrieve a variable's
name in the current scope starting from its value, could only be done
under a strict policy of assignment and nomenclature. Not something
that I would probably want to do in my code.
 
T

Thomas 'PointedEars' Lahn

Bart said:
Thomas said:
Bart said:
There simply is no 1:1 relationship between variables and
values as any variable may have any value.
At each Momentum in the code execution there is a 1:1 relation,
Huh? No, most certainly there isn't:

var x = 42;
var y = 42;
this["z"] = x;
getMe(42)
I think this is all that matters. If no variable name is
found, then it simply returns undef.
What do you want getMe() to return in this example, then?

Yes. The relation is 1-to-N, sorry.

In this example. Generally it is more like m:n, no? Never mind :)
Trying to retrieve a variable's name in the current scope starting
from its value, could only be done under a strict policy of assignment
and nomenclature. Not something that I would probably want to do in
my code.

ACK. However, I would really like to have a way to refer to the *local*
Variable Object as well.


Regards,

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top