页面调试中关于Console应该注意的地方

在近期仿网易邮箱5.0UI时,为了更方便的输出错误信息,自己在console的基础之上简单的封装了一下。代码如下:

 1 /**
 2  * 日志输入,如果开启Firebug,则直接使用console.log进行输出
 3  * @param {String|HTMLElement} msg 要输出的信息,可以是字符串,也可以是一个dom元素
 4  * @param {String} type 输出类型,默认为log,支持info/warning/error等
 5  */
 6 'log' : function(msg/* , [param, param, ... ] */, type) {
 7     var arg = [].slice.call(arguments, 0);
 8     
 9     type = arg.length < 2 ? 'log' : arg.pop();
10     
11     typeof console === 'object' && typeof console[type] === 'function' && console[type].apply(null, arg);
12     
13     return this;
14 },

以上代码在除Chrome浏览器之外一切都正常,在Chrome下总是报错于console[type].apply(null,arg)。原因在于在chrome下console的方法仅能被console本身调用。因此改为:

1 console[type].apply(console, arg);
原文地址:https://www.cnblogs.com/AUOONG/p/2796036.html