471,593 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,593 software developers and data experts.

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

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
Jun 27 '08 #1
11 21289
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
Jun 27 '08 #2
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
Jun 27 '08 #3
VK
On May 18, 10:56 pm, John G Harris <j...@nospam.demon.co.ukwrote:
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 ?
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?
Jun 27 '08 #4
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)
Jun 27 '08 #5
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....b0d99c1c66833/
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
Jun 27 '08 #6
"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
Jun 27 '08 #7
Bart Van der Donck wrote on 19 mei 2008 in comp.lang.javascript:
"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)
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #8
On May 19, 8:25 am, Bart Van der Donck wrote:
>"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)
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.
Jun 27 '08 #9
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>
Jun 27 '08 #10
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
Jun 27 '08 #11
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
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Guy Robinson | last post: by
4 posts views Thread by Richard Salin | last post: by
5 posts views Thread by Wilhelm Pieper | last post: by
2 posts views Thread by SGi | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.