【问题标题】:Custom console.log that keeps line number保留行号的自定义 console.log
【发布时间】:2014-09-08 05:31:24
【问题描述】:

我正在尝试为日志输出添加一些前缀,但在 Chrome 中效果不佳:

function getlog(p) {  
  return function() { 
  var mainArguments = [p].concat.call(arguments);
  console.log.bind(console).apply(console, mainArguments); }
}

最简单的解决方案效果很好:console.log.bind(console),但我想添加额外的文本。

相关主题:

console.log wrapper that keeps line numbers and supports most methods?

【问题讨论】:

  • 为什么不console.log.apply(console, mainArguments)
  • 然后我得到 .apply 方法的行号,而不是调用者的行号。

标签: javascript


【解决方案1】:

在非数组(甚至是类数组对象)上使用Array.prototype.concat() 会导致它将对象本身而不是其内容添加到结果数组中。您的代码实际上也没有在concat() 调用中使用[p],而只是简单地使用它来间接访问Array.prototype.concat()

试试这个:

function getlog(p) {  
    return function() { 
        var mainArguments = [p].concat(Array.prototype.slice.call(arguments));
        console.log.apply(console, mainArguments); 
    };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-12
    • 2020-09-09
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2014-02-07
    相关资源
    最近更新 更多