473,324 Members | 2,002 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,324 software developers and data experts.

Find out the name of a variable

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
Jul 23 '05 #1
8 1393
Thomas Mlynarczyk wrote:
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.
Jul 23 '05 #2
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.

Jul 23 '05 #3
Thomas Mlynarczyk wrote:
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.


var myVar = "This is my Variable";

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

go(myVar);

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #4
Also sprach Randy Webb:
Thomas Mlynarczyk wrote:
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.


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.

Jul 23 '05 #5
JRS: In article <cv*************@news.t-online.com>, dated Sat, 26 Feb
2005 14:55:45, seen in news:comp.lang.javascript, Thomas Mlynarczyk
<bl*************@hotmail.com> posted :

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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
rh
Thomas Mlynarczyk wrote:
Also sprach Randy Webb:

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


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

Jul 23 '05 #7
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.
Jul 23 '05 #8
rh
Thomas Mlynarczyk wrote:
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?


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

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lucas Sain | last post by:
Hi, I think thta for this I have to use reflection... but I'm not shure. How can I find/get an object at runtime by looking for its name that is stored in a variable. For example: I have a...
3
by: NWx | last post by:
Hi, I have a ASP.NET application using forms authentication Default page is default.aspx, and login page is login.aspx As I perform authentication in Login page, I want to update a log table...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
2
by: VMI | last post by:
In my Windows Form, is it possible to get the control name through object sender in an event handler? For example, in private void dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e), how...
1
by: kraj123 | last post by:
Hi, How to Find ,if already a environment variable is set in hash table in perl. Actually i want to check if a environmental variable in perl script, which is present in oracle database has...
3
by: Abhinavnaresh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.