Make JS debugging Easier-console

Source: Internet
Author: User

I. Commands to display information
    • Console.log for output general information
    • Console.info for output informational information
    • Console.error for output error messages
    • Console.warn for output warning messages

The most common is the console.log.

Second, placeholder

Console The above concentration supports printf placeholder format, supported placeholders are: characters (%s), integers (%d or%i), floating-point numbers (%f), and Objects (%o):

<script type= "Text/javascript" >     console.log ("%d years%d months%d days", 2011,3,26); </script>

%o,%o are used to output object objects, the normal object object, the two are no different, but the print DOM node is not the same:

Console.log ( '%o '////Console.log ('%o '),  Document.body.firstElementChild);

The%c placeholder is the most common. When using the%c placeholder, the corresponding argument must be a CSS statement that is used to render the output CSS. There are two common types of output: Text style, picture output.

Third, display the content of a node
<!DOCTYPE HTML>    <HTML>    <Head>        <title>Common Console Commands</title>        <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />    </Head>    <Body>        <DivID= "Info">            <H3>My blog: http://www.cnblogs.com/chaoran/</H3>        </Div>        <Scripttype= "Text/javascript">            varInfo=document.getElementById ('Info');        Console.dirxml (info); </Script>    </Body>    </HTML>

Iv. Judging if the variable is 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.

Console.assert asserts the input expression and outputs the appropriate information to the console only if the expression is false.

<script type= "Text/javascript" > var result = 1;   Console.assert (result);  var year = == 2018 ); </script>

1 is a value of 0, is true, and the second is false, displaying an error message on the console

Tracing the call path of the function

Console.trace () is used to track the call path of a function.

 <script type= "Text/javascript" > /*   */ 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> 

Console output Information:

Six, chronograph function

Console.time () and Console.timeend () are used to display the elapsed time of the code.

<script type= "Text/javascript" > console.time ("console timer one");  for (var i=0;i<1000;i++) {  for (var j=0;j<1000;j++) {}} Co Nsole.timeend ("console timer one"); </script>

The performance analysis of the Seven and Console.profile ()

Performance analysis (Profiler) is the analysis of the various parts of the running time, to find out the bottleneck, the method used is Console.profile ().

<script type= "Text/javascript" >functionAll () {alert (11);  for(vari=0;i<10;i++) {Funca (1000); } FUNCB (10000); }    functionFunca (count) { for(vari=0;i<count;i++){}    }    functionFUNCB (count) { for(vari=0;i<count;i++) {} console.profile (' Performance Analyzer ');      All (); Console.profileend ();
}</script>

Reference:

Chrome Console Console Usage

Nine console commands to make JS debugging easier

Make JS debugging Easier-console

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.