Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

2 views
Skip to first unread message

EricGoogle

unread,
May 16, 2008, 4:11:36 PM5/16/08
to
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

Thomas 'PointedEars' Lahn

unread,
May 16, 2008, 5:17:55 PM5/16/08
to
EricGoogle wrote:
> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

John G Harris

unread,
May 18, 2008, 2:56:08 PM5/18/08
to
On Fri, 16 May 2008 at 13:11:36, in comp.lang.javascript, EricGoogle
wrote:

>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
--
John Harris

VK

unread,
May 18, 2008, 4:02:11 PM5/18/08
to

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?

Evertjan.

unread,
May 18, 2008, 6:30:24 PM5/18/08
to
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Bart Van der Donck

unread,
May 19, 2008, 2:58:32 AM5/19/08
to
Thomas 'PointedEars' Lahn wrote:

> EricGoogle wrote:
>
>> 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.

--
Bart

Bart Van der Donck

unread,
May 19, 2008, 3:25:44 AM5/19/08
to
"Evertjan." wrote:

> 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.

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)

--
Bart

Evertjan.

unread,
May 19, 2008, 6:22:55 AM5/19/08
to

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.

Henry

unread,
May 19, 2008, 6:52:31 AM5/19/08
to

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.

Bart Van der Donck

unread,
May 19, 2008, 7:46:12 AM5/19/08
to
Henry wrote:

> On May 19, 8:25 am, Bart Van der Donck wrote:
>
>> 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 -.

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.

--
Bart

Thomas 'PointedEars' Lahn

unread,
May 19, 2008, 9:11:12 AM5/19/08
to
Bart Van der Donck wrote:
> Thomas 'PointedEars' Lahn wrote:
>> EricGoogle wrote:
>>> 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.

>> 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?


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>

Bart Van der Donck

unread,
May 19, 2008, 10:10:03 AM5/19/08
to
Thomas 'PointedEars' Lahn wrote:

> Bart Van der Donck wrote:
>
>> $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.
>
>>> 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. 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.

--
Bart

Thomas 'PointedEars' Lahn

unread,
May 19, 2008, 10:25:52 AM5/19/08
to
Bart Van der Donck wrote:
> Thomas 'PointedEars' Lahn wrote:
>> Bart Van der Donck wrote:
>>>> 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

0 new messages