Nine Console commands that make JavaScript debugging easier, javascriptconsole
1. Command for displaying information
<! DOCTYPE html>
The most common one is console. log.
2. placeholders
The preceding concentration on the console supports the placeholder format of printf. Supported placeholders include: character (% s), INTEGER (% d or % I), floating point number (% f) and object (% o)
<Script type = "text/javascript"> console. log ("% d", 2011,3, 26); </script>
Effect:
3. Information grouping
<! DOCTYPE html>
Effect:
4. view object information
Console. dir () can display all attributes and methods of an object.
<Script type = "text/javascript"> var info = {blog: "http://www.ido321.com", QQGroup: 259280570, message: "program lovers welcome to join"}; console. dir (info); </script>
Effect:
5. display the content of a node
Console. dirxml () is used to display the html/xml Code contained by a node on a webpage.
<! DOCTYPE html>
Effect:
6. Determine whether the variable is true.
Console. assert () is used to determine whether an expression or variable is true. If the result is no, a message is output on the console and an exception is thrown.
<script type="text/javascript"> var result = 1; console.assert( result ); var year = 2014; console.assert(year == 2018 );</script>
1 is a non-0 value, true, and the second is false. The error message is displayed on the console.
7. Tracing function call traces
Console. trace () is used to track the call track of a function.
<Script type = "text/javascript">/* describes how the function is called and adds it to the console. trace () method. */function add (a, B) {console. trace (); return a + B;} var x = add3 (1,1); function add3 (a, B) {return add2 (a, B);} function add2 (, b) {return add1 (a, B);} function add1 (a, B) {return add (a, B) ;}</script>
Console output information:
8. Timing Function
Console. time () and console. timeEnd () are used to display the code running time.
<Script type = "text/javascript"> console. time ("console timer 1"); for (var I = 0; I <1000; I ++) {for (var j = 0; j <1000; j ++) {}} console. timeEnd ("console timer 1"); </script>
Running time is 38.84 ms
9. Performance Analysis of console. profile ()
Performance analysis (Profiler) is used to analyze the running time of each part of the program and identify the bottleneck. The method used is console. profile ().
<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 ('performance analyzer '); All (); console. profileEnd (); </script>
Output
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.