Nine Console commands to make JS debugging easier

Source: Internet
Author: User

I. Commands to display information
    Console.log (' hello ');    Console.info (' information ');    Console.error (' error ');    Console.warn (' warning ');

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)

Console.log ("%d years%d months%d days", 2011,3,26);

Iii. Grouping of information
Console.group ("First set of Information");    Console.log ("first Group One");    Console.log ("first Group II"); Console.groupend (); Console.group ("Second set of Information")    ; Console.log ("second group first");    Console.log ("second group, second article"); Console.groupend ();

Iv. Viewing the information of an object

Console.dir () can display all properties and methods of an object.

    var info = {        qq:"332877552",        message:"hahaha",        "ddddd"    };    Console.dir (info);
V. Display the contents of a node

Console.dirxml () is used to display the Html/xml code contained in a node of a Web page.

<div id= "Info" >    var info = document.getElementById (' info ');    Console.dirxml (info); </script>

Vi. 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.

var result = 1; Console.assert (result); //1 non-0 value, true;var year = == 2018) ;  //While the second judge is false, displays an error message on the console

Tracing the call path of the function

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

    Console.log (' Print information ');     /* How the function is called, in which the Console.trace () method is added to the */    function Add (A, b) {        console.trace (        ); return A +b    ;    } var x = add3 (n);     function add3 (A, b) {return  add2 (A,    b);} function Add2 (A, b) {return  add1 (A,    b);} function add1 (A, b) {return Add (a, b);}

Eight, chronograph function

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

   Console.log (' Print information ');    Console.time ("console timer one");      for (var i=0;i<100;i++) {        for (var j=0;j<100;j++) {}    }    Console.timeend ("console timer one");

Performance analysis of 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 ().

function All () {    alert (one);      for (var i=0;i<10;i++) {        Funca (+);    }    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 ();

Nine Console commands to make JS debugging easier

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.