Get variable names with JavaScript

前端 未结 7 980
傲寒
傲寒 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: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();
    

提交回复
热议问题