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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnywhy
    New Member
    • Dec 2007
    • 9

    #1

    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:

    Code:
    function Billiard(){
         // position
         this.position;
    
         // strike another billiard
         this.strike = function(targetBilliard, strength){
              targetBilliard.position += strength;
              alert(targetBilliard.number + ' position: ' + targetBilliard.position);
            }
        }
    
    // make 2 billiards
    var b1 = new Billiard;
    b1.position = 8;
    
    var b2 = new Billiard;
    b2.position = 5;
    
    // b1 strikes b2
    b1.strike(b2, 5);
    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(targetBi lliard)

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

    thx!
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

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

    Code:
    function getVarName(val, scope) {
        var ret = null;
    	    
        for (var i in scope) {
            var r = scope[i];
    	        
            if (r === val) {
                ret = i;
                break;
            }
        }
    	    
        return ret;
    };
    	
    function Billiard() {
        // position
        this.position;
    	
        // strike another billiard
        this.strike = function(targetBilliard, strength) {
            var tgt = getVarName(targetBilliard, window);
    	    
            targetBilliard.position += strength;
    		  
            console.log(
               tgt + ' position: ' + targetBilliard.position
            );
        };
    };
    	
    // make 2 billiards
    var b1 = new Billiard;
    b1.position = 8;
    	
    var b2 = new Billiard;
    b2.position = 5;
    	
    // b1 strikes b2
    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:

    Code:
    function Billiard(name) {
        this.name = name;
    
        // ...
    }
    and then you could simply ask for:

    Code:
    targetBilliard.name
    in your code.
    Last edited by gits; Feb 11 '16, 12:09 PM.

    Comment

    • johnywhy
      New Member
      • Dec 2007
      • 9

      #3
      Brilliant, gits!

      Thx!

      Comment

      Working...