0

有没有办法制作一个为一串变量运行 console.log 的函数。运行一次以获取变量名的名称,并运行一次以显示其 val

function multilog(text){
    var text=text.split(",")
    for(i=0;i<text.length;i++){
        console.log(text[i]+': ')
        console.log(JSON.stringify(text[i]));
    }
}
multilog('number_words,number_paragraphs,relatedwords');

所需输出的示例

number_words:1 number_paragraphs:2 relatedwords:[示例示例]

4

2 回答 2

1
var logNumber = 0;

// The _log() will be a global function so you could access it from any place in the code.
// You can pass parameters like : _log( 'Something', 'Anything', '...', '...' ) 
_log = function () {
    console.log( '========= Log number : ' + logNumber + '. =========' )
    for( var i = 0, l = arguments.length; i < l; i ++ ) {
        console.log( arguments[ i ] );
    }
    console.log( '====== End of log number: ' + logNumber + '. ======' )
    logNumber ++;
}
于 2013-09-13T13:35:26.137 回答
0
console.multilog = function(str){
    var toLog = str.split(',');
    for (var i = 0, len = toLog.length; i < len; i++){
    console.log(toLog[i] + ' =>');
    console.log(eval(toLog[i]));
    }
};
于 2013-09-13T13:32:16.610 回答