接着上一篇js中console使用1,本片继续介绍js中console的用法
测试代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>Console</title>
<script type="text/javascript">
window.onload=_pageLoaded; /**
* 页面加载完毕后执行的函数
* @private
*/
function _pageLoaded() {
var num=5;
var result = factorial(num);
console.info("%d的阶乘为:%d",num,result);
//1.堆内存使用情况
console.info('当前内存信息为:%o',console.memory);
//3.断言(如果表达式或变量结果为false,则显示错误消息)
var a = 4;
console.assert(a==5,"a不等于5");
if(a!=5){
console.error("a不等于5");
}
//4.清除控制台中消息
console.clear();
console.info('已清除控制台中消息!');
//5.将对象或数组以表格的形式打印出来
var arr0 = ["张泰松","李超军","李鹏鹏"];
var arr1 = [["张","泰松"],["李","超军"],["李","鹏鹏"]];
var obj = {name:'张泰松',age:'28',address:'杭州市西湖区'};
var objArr = [{name:'张泰松',age:'28',address:'杭州市西湖区'},{name:'李超军',age:'30',address:'杭州市滨江区'}];
console.table(arr0);
console.table(arr1);
console.table(obj);
console.table(objArr); var complexObj = {};
complexObj['first' ] = {name:'张泰松',age:'28',address:'杭州市西湖区'};
complexObj['second'] = {name:'李超军',age:'30',address:'杭州市滨江区'};
complexObj['third' ] = {name:'段亚飞',age:'29',address:'北京市昌平区'};
console.table(complexObj);
console.table(objArr,['name','age']); var obj = {name:'pine',age:27};
console.info(Object.keys(obj));
console.info(Object.values(obj)); } /**
* 计算阶乘
*/
function factorial(num){
//2.对函数或代码执行次数进行计数(显示函数或代码执行次数)
console.count('执行次数');
if(num==1){
return 1;
}
return num*factorial(num-1);
} </script>
</head>
<body>
<table id="table1">
<tr>
<td>1</td><td>2</td>
</tr>
<tr>
<td>3</td><td>4</td>
</tr>
</table>
</body>
</html>