Nine console commands to make JS debugging easier

Source: Internet
Author: User

I. Commands to display information

1 <!DOCTYPE HTML>2     <HTML>3     <Head>4         <title>Common Console Commands</title>5         <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />6     </Head>7     <Body>8         <Scripttype= "Text/javascript">9 Console.log ('Hello');Ten Console.info ('Information'); One Console.error ('Error'); A Console.warn ('Warning'); -        </Script> -    </Body> the   </HTML>

The most common is the console.log.

Two: 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)

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

Effect:

Iii. Grouping of information

1 <!DOCTYPE HTML>2     <HTML>3     <Head>4         <title>Common Console Commands</title>5         <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />6     </Head>7     <Body>8         <Scripttype= "Text/javascript">9 Console.group ("first set of information");Ten    One Console.log ("First set of first: my blog (http://www.ido321.com)"); A    - Console.log ("First group Second article: CSDN (http://blog.csdn.net/u011043843)"); -    the console.groupend (); -    - Console.group ("Second set of information"); -    + Console.log ("The second group first article: Program Enthusiasts QQ Group: 259280570"); -    + Console.log ("Second group, article two: Welcome to join"); A    at console.groupend (); -        </Script> -    </Body> -    </HTML>

Effect:

Iv. Viewing the information of an object

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

1 <script type= "Text/javascript" >2             varinfo = {3                 Blog: "/http// Www.ido321.com ",4                 qqgroup:259280570,5                 message:" Program enthusiasts welcome you to join " 6             }; 7             Console.dir (info); 8     </script>

Effect:

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.

1 <!DOCTYPE HTML>2     <HTML>3     <Head>4         <title>Common Console Commands</title>5         <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />6     </Head>7     <Body>8         <DivID= "Info">9             <H3>My blog: www.ido321.com</H3>Ten            <P>Program enthusiasts: 259280570, you are welcome to join</P> One        </Div> A        <Scripttype= "Text/javascript"> - Varinfo=document.getElementById ('Info'); - Console.dirxml (info); the        </Script> -    </Body> -    </HTML>

Effect:

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.

1 <script type= "Text/javascript" >2         varresult = 1; 3         Console.assert (result); 4         Varyear =; 5         Console.assert (Year = = 2018); 6 </script>

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

Tracing the call trajectory of the function.

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

1<script type= "Text/javascript" >2     /*How the function is called, in which the Console.trace () method is added to the*/3 Functionadd (A, b) {4 console.trace ();5returna+b;6     }7Varx = ADD3 ();8 Functionadd3 (A, b) {returnadd2 (A, b);}9 Functionadd2 (A, b) {returnadd1 (A, b);}Ten Functionadd1 (A, b) {Returnadd (A, b);} One</script>

Console output Information:

Eight, chronograph function

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

1  <script type= "Text/javascript" >2     console.time ("console timer one"); 3      for (vari=0;i<1000;i++) {4      for (varj=0;j<1000;j++) {} 5     }6     console.timeend ("console timer one"); 7 </script>

Run Time is 38.84ms

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 ().

1<script type= "Text/javascript" >2 Functionall () {3Alert (11);4           for(vari=0;i<10;i++){5Funca (1000);6                  }7FUNCB (10000);8         }9   Ten Functionfunca (count) { One         for(vari=0;i<count;i++){} A        } -    - FUNCTIONFUNCB (count) { the         for(vari=0;i<count;i++){} -        } -    -Console.profile (' Performance Analyzer ')); + All (); - console.profileend (); +</script>

Output

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.