I. Commands displaying information
console.log('hello');
console.info('information');
console.error('error');
console.warn('warning');
console.trace('Overview of Display Function Execution');
Two: placeholders
- console supports placeholder format for printf in terms o f the above concentrations: characters (% s), integers (% d or% i), floating point numbers (% f) and objects (% o)
The code is as follows:
<script type="text/javascript">
console.log("%d year%d month%d day",2011,3,26);
</script>
III. Information Grouping
- Copy the code as follows:
<!DOCTYPE html>
<html>
<head>
<title>Commonly used console command</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
console.group("First set of information");
console.log("Group I Article 1:");
console.log("Group I, Article 2");
console.groupEnd();
console.group("The second group of information");
console.log("Group II, Article 1);
console.log("Group II Article 2");
console.groupEnd();
</script>
</body>
</html>
IV. Viewing Object Information
- console.dir() can display all the attributes and methods of an object.
- Copy the code as follows:
<script type="text/javascript">
var info = {
blog:"http://www.jb51.net",
QQGroup:111111,
message:"Programmer"
};
console.dir(info);
</script>
5. Display the content of a node
- console.dirxml() is used to display html/xml code contained in a node of a web page.
Copy the code as follows:
<!DOCTYPE html>
<html>
<head>
<title>Commonly used console command</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="info">
<h3>My blog</h3>
<p>Programmer</p>
</div>
<script type="text/javascript">
var info = document.getElementById('info');
console.dirxml(info);
</script>
</body>
</html>
6. Is the variable true?
- console.assert() is used to determine whether an expression or variable is true. If the result is no, a corresponding message is output in the console and an exception is thrown.
Copy the code as follows:
<script type="text/javascript">
var result = 1;
console.assert( result );
var year = 2014;
console.assert(year == 2018 );
</script>
- 1 is a non-zero value, is true; and the second judgement is false, display error information in the console
Tracking the call trajectory of the function.
- console.trace() is used to track the call trajectory of a function.
Copy the code as follows:
<script type="text/javascript">
/*How is the function called? Just add the console.trace() method to it.*/
function add(a,b){
console.trace();
return a+b;
}
var x = add3(1,1);
function add3(a,b){return add2(a,b);}
function add2(a,b){return add1(a,b);}
function add1(a,b){return add(a,b);}
</script>
VIII. Timing Function
- console.time() and console.timeEnd() are used to display the running time of the code.
- Copy the code as follows:
<script type="text/javascript">
console.time("Console timer I");
for(var i=0;i<1000;i++){
for(var j=0;j<1000;j++){}
}
console.timeEnd("Console timer I");
</script>
The running time is 38.84 Ms.
9. Performance analysis of console.profile()
- Profiler is to analyze the running time of each part of the program and find out the bottleneck. The method used is console.profile().
Copy the code as follows:
<script type="text/javascript">
function All(){
alert(11);
for(var i=0;i<10;i++){
funcA(1000);
}
funcB(10000);
}
function funcA(count){
for(var i=0;i<count;i++){}
}
function funcB(count){
for(var i=0;i<count;i++){}
}
console.profile('Profiler');
All();
console.profileEnd();
</script>
- To illustrate, in LZ test, alert is not added to All(), control bar is not output, and after that, there is a performance analysis table, which is not clear for the time being. If you know, you can comment.