js中console使用2
接着上一篇js中console使用1,本片继续介绍js中console的用法
测试代码如下:
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Console</title> 7 <script type="text/javascript"> 8 window.onload=_pageLoaded; 9 10 /** 11 * 页面加载完毕后执行的函数 12 * @private 13 */ 14 function _pageLoaded() { 15 var num=5; 16 var result = factorial(num); 17 console.info("%d的阶乘为:%d",num,result); 18 //1.堆内存使用情况 19 console.info('当前内存信息为:%o',console.memory); 20 //3.断言(如果表达式或变量结果为false,则显示错误消息) 21 var a = 4; 22 console.assert(a==5,"a不等于5"); 23 if(a!=5){ 24 console.error("a不等于5"); 25 } 26 //4.清除控制台中消息 27 console.clear(); 28 console.info('已清除控制台中消息!'); 29 //5.将对象或数组以表格的形式打印出来 30 var arr0 = ["张泰松","李超军","李鹏鹏"]; 31 var arr1 = [["张","泰松"],["李","超军"],["李","鹏鹏"]]; 32 var obj = {name:'张泰松',age:'28',address:'杭州市西湖区'}; 33 var objArr = [{name:'张泰松',age:'28',address:'杭州市西湖区'},{name:'李超军',age:'30',address:'杭州市滨江区'}]; 34 console.table(arr0); 35 console.table(arr1); 36 console.table(obj); 37 console.table(objArr); 38 39 var complexObj = {}; 40 complexObj['first' ] = {name:'张泰松',age:'28',address:'杭州市西湖区'}; 41 complexObj['second'] = {name:'李超军',age:'30',address:'杭州市滨江区'}; 42 complexObj['third' ] = {name:'段亚飞',age:'29',address:'北京市昌平区'}; 43 console.table(complexObj); 44 console.table(objArr,['name','age']); 45 46 var obj = {name:'pine',age:27}; 47 console.info(Object.keys(obj)); 48 console.info(Object.values(obj)); 49 50 } 51 52 /** 53 * 计算阶乘 54 */ 55 function factorial(num){ 56 //2.对函数或代码执行次数进行计数(显示函数或代码执行次数) 57 console.count('执行次数'); 58 if(num==1){ 59 return 1; 60 } 61 return num*factorial(num-1); 62 } 63 64 65 66 </script> 67 </head> 68 <body> 69 <table id="table1"> 70 <tr> 71 <td>1</td><td>2</td> 72 </tr> 73 <tr> 74 <td>3</td><td>4</td> 75 </tr> 76 </table> 77 </body> 78 </html>