473,839 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1441
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.javas cript 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
1656
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 DataTable called dtPersons that loads all the data of the persons. I have a string variable called tableVariable that stores tha name "dtPersons".
3
6835
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 with both successful and unsuccessful logins. As usual, client computer connects first to default.aspx, but since it isn't authenticated, it is redirected to login.aspx.
2
5051
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 folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab ->...
17
3040
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). I have a dataset as follows (1 week to keep it short): Employee 1 - Date 1 Employee 1 - Date 2
5
2314
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 System.CodeDom.CodeCastExpression) __problem typecast #1 Desc:i do needed checks but data/commands in XML is dynamic and i don't wanna fix C# code again and again... Sample:foreach (object some in somearray) (some.GetType())some.someaction();
1
4221
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: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
2
37160
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 can I know that the sender is "dataGridView_zip" ? Thanks. VS2005 2.0
1
3802
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 been already set its path in hash table in perl. that is i have to query the database server and check whether it has been set as environment variable or not. so basically i want to check with if statment to sdatabase erver name and a loop to...
3
7896
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. Exception Details: System.Xml.Xsl.XsltException: Cannot find the script or external object that implements prefix 'counter'. Source Error: An unhandled exception was generated during the execution of the current web request. Information...
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10914
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10597
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10656
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.