console.log在webkit中apply

为了调试方便,我们有时候会这样写log()

 1 /** @define {boolean} */
2 var DEBUG = true;
3
4 function log() {
5 if (DEBUG) {
6 console.log.apply(this, arguments);
7 }
8 }
9
10 function foo(bar) {
11 log('bar is %s.', bar);
12 }

发布代码的时候只要在Closure Compiler的参数里设置 DEBUG=false 就不用手动删除每一条log()调用了。

但是上面的代码在webkit里面会报错:

Uncaught TypeError: Illegal invocation

其实需要这么写:

console.log.apply(console, arguments);



原文地址:https://www.cnblogs.com/dishuostec/p/2286327.html