狄烁STEC的试验场

hello earth

导航

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);



posted on 2011-12-13 16:55  狄烁STEC  阅读(1552)  评论(0编辑  收藏  举报