Get variable names with JavaScript

前端 未结 7 901
傲寒
傲寒 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

提交回复
热议问题