Find out the name of a variable

T

Thomas Mlynarczyk

Hi,

I want to pass a variable to a function and that function should display
both the value and the name of that variable. As for the value - no problem,
but the name? So far I've come up with

function foo() {
var myVar = 5;
var myOtherVar = 'bla';
showVars( {myVar: myVar, myOtherVar: myOtherVar} );
}

function showVars(oVars) {
for(var sName in oVars) {
// Display sName and oVars[sName]
}
}

But this {myVar: myVar} is a bit awkward as I have to write everything
twice. Is there a more elegant way?

Greetings,
Thomas
 
R

Richard Cornford

Thomas said:
I want to pass a variable to a function

The arguments that are passed to a function are values; either primitive
values or references to objects. Variables are named properties of
objects in the scope chain that (may) have a value, not distinct units
that can be passed around. And there is certainly no relationship from
that value held as a named property of an object to the name used for
that property; there could never be such a relationship because many
object properties may refer to the same value.
and that function should display both the value and
the name of that variable. As for the
value - no problem, but the name? So far I've come up with

function foo() {
var myVar = 5;
var myOtherVar = 'bla';
showVars( {myVar: myVar, myOtherVar: myOtherVar} );
}

function showVars(oVars) {
for(var sName in oVars) {
// Display sName and oVars[sName]
}
}

But this {myVar: myVar} is a bit awkward as I have to
write everything twice. Is there a more elegant way?

Outside of the very specific area of debugging, there is no reason to be
interested in the names of variables that hold values. This is a case
where progress will only be achieved here following an explanation of:
_why_?

Richard.
 
T

Thomas Mlynarczyk

Also sprach Richard Cornford:
Outside of the very specific area of debugging, there is no reason to
be interested in the names of variables that hold values. This is a
case where progress will only be achieved here following an
explanation of: _why_?

It is indeed for debugging purposes - I want to call a debug output
function, pass it some variables and the function is to display their names
and values so I can see which value and type each variable has at that
moment.
 
R

Randy Webb

Thomas said:
Also sprach Richard Cornford:




It is indeed for debugging purposes - I want to call a debug output
function, pass it some variables and the function is to display their names
and values so I can see which value and type each variable has at that
moment.

var myVar = "This is my Variable";

function go(varName){
alert('Should it give you varName or myVar?');
}

go(myVar);
 
T

Thomas Mlynarczyk

Also sprach Randy Webb:
var myVar = "This is my Variable";

function go(varName){
alert('Should it give you varName or myVar?');
}

go(myVar);

It should give me an output like "myVar = 'This is my Variable'". As it
would if I used the syntax go({myVar:myVar}) and used a for-in on the
varName in the function. Only I wish there was a syntax possible that does
not require writing the variable name twice.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 26 Feb
2005 14:55:45, seen in Thomas Mlynarczyk
I want to pass a variable to a function and that function should display
both the value and the name of that variable. As for the value - no problem,
but the name?

The name is not passed.


So you have
function Tom(X, ...) {...}
and at the time of a call
Par = 3 ; Tom(Par, ...)
you want to display something like
Tom(Par=3, ...)

Replace the call to
Tom(Par, ...)
with
Cat('Tom', 'Par', ...)
which a good regexp-using editor should be able to do and undo.

A function Cat can now write Tom and its parameter names just as in the
code, and can use, I think, standard addressing techniques to write the
values and to perform the function call originally intended.


Or you can replace the call with
Evil("Tom(Par, ...)")
A function Evil can write the original call; parse to determine the
values of the parameters, and insert those in Par=3 fashion; and use
standard exec or otherwise to make the original call.


In each case, Tom should be assumed to maybe return something; so it
should itself be called as part of a return statement, or in a way that
displays and returns its result.


AFAICS, something like one or both of those should work at least for the
ordinary sort of function calls such as could be written in many
languages; but not necessarily for everything. ALL UNTESTED.
 
R

rh

Thomas said:
Also sprach Randy Webb:



It should give me an output like "myVar = 'This is my Variable'". As it
would if I used the syntax go({myVar:myVar}) and used a for-in on the
varName in the function. Only I wish there was a syntax possible that does
not require writing the variable name twice.

An alternative is to provide callback function to get the values:

<script type="text/javascript">

var myVar = "This is my Variable";

function foo(varName) {
var a = "this is a";
var b = "this is b";
showVars("a b varName", function(n) { return eval(n) });
}

function showVars(varStr, getVal) {
var vars = varStr.split(/\s+/);
if (vars) {
for (var k=vars.length; k--;) {
if(vars[k]) vars[k] = vars[k] +": "+ getVal(vars[k]);
}
alert( vars.join("\n"));
}
}

foo(myVar);

</script>

For debugging, presumably you would keep a template, e.g.,

showVars("", function(n) { return eval(n) });

which would be pasted, and variable names of interest at that point
inserted within the quotes.

Still a bit awkward, but there should be less typing required.

../rh
 
T

Thomas Mlynarczyk

Also sprach rh:
var myVar = "This is my Variable";
function foo(varName) {
var a = "this is a";
var b = "this is b";
showVars("a b varName", function(n) { return eval(n) });
}
function showVars(varStr, getVal) {
var vars = varStr.split(/\s+/);

Can split really take a regex?
if (vars) {
for (var k=vars.length; k--;) {
if(vars[k]) vars[k] = vars[k] +": "+ getVal(vars[k]);
}
alert( vars.join("\n"));
}
}
foo(myVar);

Interesting idea, I had not thought of that. Thanks for the example code.
 
R

rh

Thomas said:
Also sprach rh:


Can split really take a regex?

Yes, the separator parameter of split can be of type "string" or
"RegExp" under ECMA 262/3.

So, if you wished to allow commas as well as spaces for separators you
could use:

var vars = varStr.split(/[\s,]+/);

That would allow cut and paste from "var" statements (at least those
that don't contain initial assignment expressions).
if (vars) {
for (var k=vars.length; k--;) {
if(vars[k]) vars[k] = vars[k] +": "+ getVal(vars[k]);
}
alert( vars.join("\n"));
}
}
foo(myVar);

Interesting idea, I had not thought of that. Thanks for the example
code.

You're welcome.

../rh
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top