我正在尝试创建一个日志代理,其中每个日志语句都有一个前缀.
我想要做的是:
customDebug("xxx","yyy");
Run Code Online (Sandbox Code Playgroud)
在木头下它会做:
console.debug("prefix","xxx","yyy");
Run Code Online (Sandbox Code Playgroud)
我试过这样实现它:
prefixLogArguments: function(arg) {
var array = _.toArray(arg);
array.unshift( this.getPrefix() );
return array;
},
customDebug: function() {
var argArray = this.prefixLogArguments(arguments);
console.debug.apply(undefined, argArray );
},
Run Code Online (Sandbox Code Playgroud)
它说Uncaught TypeError: Illegal invocation 因为看起来我们无法apply/call使用未定义的上下文调用本机代码.
有人能告诉我如何实现这一目标吗?
我可以做到console.debug(argArray);这一点并不是那么糟糕,但它不会产生与记录数组而不是记录参数列表相同的结果.
您应该将'console'作为apply的上下文:
var customDebug = function() {
console.debug.apply(console, arguments);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
581 次 |
| 最近记录: |