Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts

    Is there a way to get the name of a variable?

    Hi all,

    I need to get the name of a Javascript variable. For example, it would work like this:

    Code:
        var test = document.getElementById('whatever');
        var name = getName(test);
        alert('The variable name is ' + name);
    Running this code would result in an alert box that says "The variable name is test".

    What I need to know is how to implement the "getName()" function.

    Thanks!

    -- Roger
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Sense????

    If you write getName(test) you can as well write
    Code:
    var name = 'test';
    and you have the name of the variable test

  • #3
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by devnull69 View Post
    Sense????

    If you write getName(test) you can as well write
    Code:
    var name = 'test';
    and you have the name of the variable test
    You forgot the smiley after your post.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    + :-)

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    i don't understand why others posted jokes, this is a serious question...
    Reflectivity is one area where you need to get creative using javascript since it doesn't have much built-in.

    Code:
    function getName(x){
      for(var i in this){
         if(x=== this[i]){return i;}
      }//next
    }//end getName()
    
    
    var test = document.body;
    var name = getName(test);
    alert('The variable name is ' + name);
    note that with primitives (string, number, and booleans), getName() might yield false matches, but it still will tell you what the var's name COULD be.
    example:

    Code:
    function getName(x){
      for(var i in this){
         if(x=== this[i]){return i;}
      }//next
    }//end getName()
    
    
    
    var test = {a:1, b:2, c:3, d:3};
    
    var name = getName.call(test, test.d);
    alert('The variable name is ' + name);
    Last edited by rnd me; 07-18-2011 at 09:14 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Sorry, but I don't get that ... can you give an example for when you can not replace getName(test) with just 'test'?

    You'll have to write the variable name in your code anyway, so you can just write the name as a string literal.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    Quote Originally Posted by devnull69 View Post
    Sorry, but I don't get that ... can you give an example for when you can not replace getName(test) with just 'test'?
    things can have more than one name in js, especially byRef vars like objects and functions...

    Code:
    function getName(x){
      for(var i in this){
         if(x=== this[i]){return i;}
      }//next
    }//end getName()
    
    
    function sum(a,b){return a+b || 0;};
    
    var test = {a:1, b:2, c:sum, d:3};
    
    var name = getName.call(test, sum);
    alert('The variable name is ' + name);


    or

    Code:
    function getName(x){
      for(var i in this){
         if(x=== this[i]){return i;}
      }//next
    }//end getName()
    
    
    var o={a:1, b:2, c:3, d:3};
    
    var test = [o.a,o.b,o.c,o.d];
    
    var anon=test[Math.floor(test.length * Math.random())]
    
    var name = getName.call(o, anon);
    alert('The variable name is ' + name);

    additionally, it's easy to imaging having to sniff event handlers and other common bound methods, without having the luxury of early definition.
    well, it's easy for me to imagine; i guess if you only work with your own code in isolation you can hard-code all your names...
    Last edited by rnd me; 07-18-2011 at 10:00 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Ok, but I bet this is far away from what the OP had in mind. Still you gave a correct answer :-)

  • #9
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rnd me View Post
    i don't understand why others posted jokes, this is a serious question...
    Reflectivity is one area where you need to get creative using javascript since it doesn't have much built-in.
    Thank you. It was indeed a serious question. What I have is a small piece of code that I use to examine everything attached to a particular element. For example, if I get the element of an image on my website with document.getElementById('logo'), I can then pass it to my code and see something like this:

    Code:
    e.querySelector type: function, code: function querySelector() {\n »    [native code]\n }
    e.querySelectorAll type: function, code: function querySelectorAll() {\n »    [native code]\n }
    e.scrollTop type: number, value: 0
    e.scrollLeft type: number, value: 0
    e.scrollHeight type: number, value: 69
    e.scrollWidth type: number, value: 102
    e.clientTop type: number, value: 0
    e.clientLeft type: number, value: 0
    e.clientHeight type: number, value: 69
    e.clientWidth type: number, value: 102
    e.firstElementChild type: object, value: [object HTMLImageElement]
    e.lastElementChild type: object, value: [object HTMLImageElement]
    e.previousElementSibling type: object, value: null
    e.nextElementSibling type: object, value: [object HTMLHeadingElement]
    e.childElementCount type: number, value: 1
    e.children type: object, value: [object HTMLCollection]
    ........ more........
    
    However, the "e" is a text character that I have to pass to the function.... to get the above listing I do this:

    Code:
        var e = document.getElementById('logo');
        showElem(e, 'e');
    What I want to do is just pass the e element and have the code generate the 'e' text.

    The reason is that I may pass more than just a single letter. I might do this:

    Code:
        var e = document.getElementById('logo');
        showElem(e.style, 'e.style');
    ...which results in this:

    Code:
    e.style.azimuth type: string, value: ''
    e.style.background type: string, value: ''
    e.style.backgroundAttachment type: string, value: ''
    e.style.backgroundColor type: string, value: ''
    e.style.backgroundImage type: string, value: ''
    e.style.backgroundPosition type: string, value: ''
    e.style.backgroundRepeat type: string, value: ''
    e.style.border type: string, value: ''
    e.style.borderCollapse type: string, value: ''
    e.style.borderColor type: string, value: ''...... more ........
    Hope this makes sense....

    -- Roger
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •