Get variable names with JavaScript

前端 未结 7 902
傲寒
傲寒 2021-02-08 18:47

I want to create a log function where I can insert variable names like this:

var a = \'123\',
    b = \'abc\';

log([a, b]);

And the result sho

相关标签:
7条回答
  • 2021-02-08 19:11

    Don't know if this would really work in JS... but you can use a Object, in which you can store the name and the value:

      function MyLogObject(name, value) {
        this.name = name;
        this.value = value;
      }
    
    
      var log = [];
      log.push(new MyLogObject('a', '123'));
      log.push(new MyLogObject('b', 'abc'));
    
      for each (var item in log) {
        if (item.value != undefined)
          alert(item.name + "/" + item.value);       
      }
    

    Then you can loop thru this Object and you can get the name and the value

    0 讨论(0)
  • 2021-02-08 19:17

    I know you want to save some keystrokes. Me too. However, I usually log the variable name and values much like others here have already suggested.

    console.log({a:a, b:b});
    

    If you really prefer the format that you already illustrated, then you can do it like this:

    function log(o) {
        var key;
        for (key in o) {
            console.log(key + ":", o[key]);
        }
    }
    
    var a = '1243';
    var b = 'qwre';
    log({
        a:a,
        b:b
    });
    

    Either way, you'd need to include the variable name in your logging request if you want to see it. Like Gareth said, seeing the variable names from inside the called function is not an option.

    0 讨论(0)
  • 2021-02-08 19:18

    I had a somewhat similar problem, but for different reasons.

    The best solution I could find was:

    MyArray = ["zero","one","two","three","four","five"]; 
    MyArray.name="MyArray";
    

    So if:

    x=MyArray.name;
    

    Then:

    X=="MyArray"
    

    Like I said, it suited my needs, but not sure HOW this will work for you. I feel silly that I even needed it, but I did.

    0 讨论(0)
  • 2021-02-08 19:24

    You can't access the variable names using an Array. What you could do is use objects or pass the variable names as a String:

    var x = 7;
    var y = 8;
    
    function logVars(arr){
        for(var i = 0; i < arr.length; i++){
            alert(arr[i] + " = " + window[arr[i]]);
        }
    }
    
    logVars(["x","y"]);
    
    0 讨论(0)
  • 2021-02-08 19:27

    test this.

    var variableA="valor01"; <br>
    var variableB="valor02";
    
    var NamevariableA=eval('("variableA")');<br>
    var NamevariableB=eval('("variableB")');<br>
    
    console.log(NamevariableA,NamevariableB);
    

    atte. Manuel Retamozo Arrué

    0 讨论(0)
  • 2021-02-08 19:33

    Something like this would do what you're looking for:

    function log(logDict) {
        for (var item in logDict) {
            console.log(item + ": " + logDict[item]);
        }
    }
    
    function logSomeStuff() {
        var dict = {};
        dict.a = "123";
        dict.b = "abc";
        log(dict);
    }
    
    logSomeStuff();
    
    0 讨论(0)
提交回复
热议问题