一次偶然的机会,打开百度的时候按下了F12,而后就见控制台里面输出了百度的招聘广告,感受挺帅气的,再而后就有了这篇博文。数组
既然能够这样在控制台输出信息,那之后再调试Js的时候不就能够省去不少麻烦了嘛!避免不误人子弟,特地使用for(var i in console)查看了下各类浏览器控制台对console的支持,浏览器
结果以下:测试
IE 11 控制台插件
log , info , warn , error , debug , assert , time , timeEnd , group , groupCollapsed , groupEnd , trace , clear , dir , dirxml , count , countReset , cd , select , profile , profileEnddebug
Firebug 控制台调试
log , debug , info , warn , exception , assert , dir , dirxml , trace , group , groupCollapsed , groupEnd , profile , profileEnd , count , clear , time , timeEnd , timeStamp , table , error
Chrome 控制台日志
memory , _commandLineAPI , debug , error , info , log , warn , dir , dirxml , table , trace , assert , count , markTimeline , profile , profileEnd , time , timeEnd , timeStamp , timeline , timelineEnd , group , groupCollapsed , groupEnd , clearxml
能够看出,以上我测试的浏览器对 log , info , warn , error , debug 五个基本方法都是支持的,注意,我使用的是 IE 11,其余版本我没测试,而 Firefox 自己也是不带控制台的,须要加载Firebug 插件而且启动它,才能console,不然就是Js报错了。为了使用起来更方便,能够本身封装一下,判断一下浏览器对 console 的支持,不支持就只能使用原始的 alert 或者其余方法了。it
简单用法:io
console.log("日志信息");
console.info("通常信息");
console.debug("调试信息");
console.warn("警告提示");
console.error("错误提示");
格式化输出:
console.log("%d年%d月%d日", 2014, 5, 20);//日期格式输出
console.log('%c有颜色的输出信息', 'color:white; background-color:#0055CC');//格式输出
输出变量:
var who= 'you';
console.log('输出变量 We support ', you);//读取变量
输出数组:
var arr = [1, 2, 3, 4, 5]; console.log('数组:', arr);//输出数组