Debugging JS using the console command

Source: Internet
Author: User
Tags assert stack trace

First, the object of the console commonly used methods

1,Console.log (object[, Object, ...])
Use one of the most frequently used statements: output a message to the console. Support for C-language printf formatted output. Of course, you can do the same without using formatted output.

2, Console.debug (object[, Object, ...])
Outputs a message to the console that includes a hyperlink to the location of the line's code.

3, Console.info (object[, Object, ...])
Outputs a message to the console that contains an icon that represents "information", and a hyperlink to the location of the line code.

4, Console.warn (object[, Object, ...])

Same info. The difference is that the icon differs from the style.

5, Console.error (object[, Object, ...])

Same info. The difference is that the icon differs from the style. The error actually produces the same effect as the throw new error (), which throws a JS exception to the browser when using the statement.

6, Console.assert (expression[, Object, ...])

Assertion, tests whether an expression is true, throws an exception if it is not true (assertion fails).

7, Console.dir (object)

Outputs all the properties of an object (the output is similar to the style in the DOM panel).

8, Console.dirxml (node)
To output a tree of HTML or XML elements, click on the nodes above the tree to enter the HTML panel.

9, Console.trace ()
The stack trace at the time of the output Javascript execution.

10, Console.group (object[, Object, ...])
A nested block is opened at the same time as the output message to indent the contents of the output. Call Console.groupend () to end the output of this block.

11, console.groupcollapsed ()
With Console.group (); The difference is that nested blocks are closed by default.

12, Console.time (name)
Timer, when calling Console.timeend (name), and passing the same name as the parameter, the timing stops, and the output consumes time (in milliseconds) between executing the code between the two statements.

13, Console.profile ([title])
Used in conjunction with Profileend () for performance testing, exactly the same as the Profile button on the console panel.

14, Console.count ([title])
Output the number of times the line code is executed, and the title of the parameter will be used as the prefix for the output when it is output.

15, Console.clear ()
Emptying the console

Second, using the console debugging JS script instance

1, Console.log ()

The simplest method is Console.log (), which can be used to replace alert () or document.write ().

In addition, depending on the nature of the information, the console object also has 4 ways to display information, namely General Information Console.info (), Error console.debug (), Warning Console.warn (), Error message Console.error ().

For example

Console.info ("This is Info");

Console.debug ("This is Debug");

Console.warn ("This is warn");

Console.error ("This is Error");

When loading, the console will display the following:

2. Placeholder

You can use the printf-style placeholder for the top 5 methods of the console object. However, there are fewer types of placeholders, only support for characters (%s), integers (%d or%i), floating-point numbers (%f), and Objects (%o) four.

For example

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

Console.log ("Pi is%f", 3.1415926);

The%o placeholder, which you can use to view the inside of an object. For example, having such an object

var dog = {};

Dog.name = "Mao";

Dog.color = "Yellow";

Use of the%o placeholder

Console.log ("%o", dog);

Show

3. Console.dir () View information about an object

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

For example

    <Type= "Text/javascript">var= Blog:"ddd" Tel:  25422223 message:" Welcome to your use " }</script>     

4, Console.dirxml () display the contents of a node

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

For example

var table = document.getElementById ("table1");

Console.dirxml (table);

5, Console.assert () 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 corresponding message is output in the console and an exception is thrown.

For example

var result = 1;

Console.assert (result);

var year = 2000;

Console.assert (Year = = 2011);

6, Console.trace ()

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

7, Console.time () and Console.timeend ()

Used to display the run time of the code.

For example

Console.time ("Timer one");

for (Var i=0;i<1000;i++) {

for (Var j=0;j<1000;j++) {}

}

Console.timeend ("Timer one");

Effect

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

For example

Console.profile (' Performance Analyzer One ');

Foo ();

Console.profileend ();

Debugging JS using the console command

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.