Nine methods that web programmers must know about in the Console object

Source: Internet
Author: User
Tags assert
a command to display information
<! DOCTYPE html>
    

The most common is the console.log. Two: Placeholder

Console The above centralization supports printf placeholder formatting, supported placeholders include: Character (%s), Integer (%d or%i), floating-point number (%f), and object (%o)

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

Effect:

iii. Grouping of information

<! DOCTYPE html>
    

Effect:

Four, view the object's information

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

<script type= "Text/javascript" >
            var info = {
                blog: "http://www.webhek.com",
                qqgroup:259280570, Message
                : "Program enthusiasts welcome you to join"
            };
            Console.dir (info);
    </script>

Effect:

Show the contents of a node

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

<! DOCTYPE html>
    

Judge whether 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 printed 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 not 0 value, is true, and the second is false, the error message is displayed on the console

Seven, tracking function of the call trajectory.

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

<script type= "Text/javascript" >
    /* functions are invoked, in which the Console.trace () method can be added to the */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:

Eight, timing function

Console.time () and Console.timeend () 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++) {}
    }
    console.timeend ("console timer one");
    </script>

Run Time is 38.84ms

Performance analysis of Console.profile ()

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

<script type= "Text/javascript" >
        function All () {
                alert (one);     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 as shown:

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.