473,222 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 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 21598
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global...
7
by: Guy Robinson | last post by:
Hello, I have a directory of python scripts that all (should) contain a number of attributes and methods of the same name. I need to import each module, test for these items and unload the...
4
by: Richard Salin | last post by:
Hi I just wondered how I can refer to the following two textboxes in this form: <FORM action="FormWrite.php" method="post" name="submitForm"> <INPUT NAME="inputValues" SIZE=40> <INPUT...
5
by: Wilhelm Pieper | last post by:
Hello, HowTo: catch name/value pairs from request.form? My viewstate shows: "__VIEWSTATE=..&111=5.." I want to get this pairs of IDs/names and values . But because the DropDownList ist...
4
by: Vlady | last post by:
Hello. I recently built an ASP.NET "financial" application. I got to the point where I have a HTML template (a contract) which needs to be filled with various data from the database. I put a...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
41
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for...
2
by: SGi | last post by:
Is there any way of getting a variable name from a cstring? For example, intArray = string_to_var("intVar_1"); something like this. I'm not up to date on C++ standards, but I can swear I saw...
2
by: Joey | last post by:
I am querying a DataSet with LINQ. I am running into a problem when trying to construct my query because in the "from" clause I do not know the table name (range variable) until runtime. Possible...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.