473,396 Members | 1,693 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,396 software developers and data experts.

Is It Possible to Retrieve A Variable Name of a Parameter Passed to a Function?

Hi

My question is NOT the topic covered in this thread:
https://bytes.com/topic/javascript/answers/809155-howto-get-variables-name-not-value-introspection

Here's my scenario:

Expand|Select|Wrap|Line Numbers
  1. function Billiard(){
  2.      // position
  3.      this.position;
  4.  
  5.      // strike another billiard
  6.      this.strike = function(targetBilliard, strength){
  7.           targetBilliard.position += strength;
  8.           alert(targetBilliard.number + ' position: ' + targetBilliard.position);
  9.         }
  10.     }
  11.  
  12. // make 2 billiards
  13. var b1 = new Billiard;
  14. b1.position = 8;
  15.  
  16. var b2 = new Billiard;
  17. b2.position = 5;
  18.  
  19. // b1 strikes b2
  20. b1.strike(b2, 5);
  21.  
What i'd like to happen is, in the Strike function, when b1 strikes b2, display the variable-name of the targetBilliard in the alert, "b2".

Something like:
NameOf(targetBilliard)

i realize this may not be possible, but i'm guessing would require some sort of memory access?

thx!
Jan 14 '16 #1

✓ answered by gits

you can retrieve that by implementing a function like the following (getVarName):

Expand|Select|Wrap|Line Numbers
  1. function getVarName(val, scope) {
  2.     var ret = null;
  3.  
  4.     for (var i in scope) {
  5.         var r = scope[i];
  6.  
  7.         if (r === val) {
  8.             ret = i;
  9.             break;
  10.         }
  11.     }
  12.  
  13.     return ret;
  14. };
  15.  
  16. function Billiard() {
  17.     // position
  18.     this.position;
  19.  
  20.     // strike another billiard
  21.     this.strike = function(targetBilliard, strength) {
  22.         var tgt = getVarName(targetBilliard, window);
  23.  
  24.         targetBilliard.position += strength;
  25.  
  26.         console.log(
  27.            tgt + ' position: ' + targetBilliard.position
  28.         );
  29.     };
  30. };
  31.  
  32. // make 2 billiards
  33. var b1 = new Billiard;
  34. b1.position = 8;
  35.  
  36. var b2 = new Billiard;
  37. b2.position = 5;
  38.  
  39. // b1 strikes b2
  40. b1.strike(b2, 5);
what do we use here? you register the variables b1, b2 globally in the window scope. the function getVarName makes use of that now and retrieves the property names of the scope and if it finds an identical object in that scope it returns the property's name.

cheers :)

PS: its working here because objects are assigned to the variables in question - if it would be primitive values then this wouldn't work correctly because such values are not passed by reference in JavaScript. Basically i would give the objects a name when constructing them so it would be much easier to identify them for example:

Expand|Select|Wrap|Line Numbers
  1. function Billiard(name) {
  2.     this.name = name;
  3.  
  4.     // ...
  5. }
and then you could simply ask for:

Expand|Select|Wrap|Line Numbers
  1. targetBilliard.name
in your code.

2 1506
gits
5,390 Expert Mod 4TB
you can retrieve that by implementing a function like the following (getVarName):

Expand|Select|Wrap|Line Numbers
  1. function getVarName(val, scope) {
  2.     var ret = null;
  3.  
  4.     for (var i in scope) {
  5.         var r = scope[i];
  6.  
  7.         if (r === val) {
  8.             ret = i;
  9.             break;
  10.         }
  11.     }
  12.  
  13.     return ret;
  14. };
  15.  
  16. function Billiard() {
  17.     // position
  18.     this.position;
  19.  
  20.     // strike another billiard
  21.     this.strike = function(targetBilliard, strength) {
  22.         var tgt = getVarName(targetBilliard, window);
  23.  
  24.         targetBilliard.position += strength;
  25.  
  26.         console.log(
  27.            tgt + ' position: ' + targetBilliard.position
  28.         );
  29.     };
  30. };
  31.  
  32. // make 2 billiards
  33. var b1 = new Billiard;
  34. b1.position = 8;
  35.  
  36. var b2 = new Billiard;
  37. b2.position = 5;
  38.  
  39. // b1 strikes b2
  40. b1.strike(b2, 5);
what do we use here? you register the variables b1, b2 globally in the window scope. the function getVarName makes use of that now and retrieves the property names of the scope and if it finds an identical object in that scope it returns the property's name.

cheers :)

PS: its working here because objects are assigned to the variables in question - if it would be primitive values then this wouldn't work correctly because such values are not passed by reference in JavaScript. Basically i would give the objects a name when constructing them so it would be much easier to identify them for example:

Expand|Select|Wrap|Line Numbers
  1. function Billiard(name) {
  2.     this.name = name;
  3.  
  4.     // ...
  5. }
and then you could simply ask for:

Expand|Select|Wrap|Line Numbers
  1. targetBilliard.name
in your code.
Feb 11 '16 #2
Brilliant, gits!

Thx!
Feb 16 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
2
by: Koen Van Herck | last post by:
For debugging/logging purposes, I have a function def Log(msg): print '%s.%s: %s' % (cls, method, msg) I call this function from a class method, and I would like to retrieve the name of the...
1
by: Luke Airig | last post by:
I am using the Saxon 6.5.3 engine and I have an xsl stylesheet that merges two files on a date_time field and writes out a tab-delimited flat file. My working version has a hard-coded file name in...
7
by: Adam | last post by:
I've cobbled a sort of global "debug" routine by using this function: function debug($str, $strname) { global $debugYN; if ($debugYN) { echo "<br><br>"; } } This function gets included...
6
by: Jody Gelowitz | last post by:
I have run into an issue with variable scope within an XSLT document that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this code works fine. However, when using VS.NET 2.0...
1
by: tomjbr.32022025 | last post by:
I have started looking at the nhibernate framework, but do not really like the string based API which makes it impossible to use automatic refactoring of a property name without the risk of getting...
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
1
by: artev | last post by:
If I have this function function fun_p(variable) { alert (variable.name); } with Dom, when I click on a text mytext, I have the mytext's name only if I write so:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.