Use the console to debug javascrip

Source: Internet
Author: User

1. What is console?
Console is a separate window used to display JS and DOM object information. And inject a console object to Js.
You can output information to the console window.
 
2. What browsers support console?
Many people may know that chrome and Firefox support the console. Other browsers support
Hold bad. For example, although the development tool provided by IE8 supports the console, its functions are monotonous and display objects.
[Object, object], and you cannot click to view the attributes in the object. IE6 and IE7 can install the developer toolbar,
Console is not supported. Both safari and Opera support the console, but there is no firebug or chrome convenience in use.
Currently, firebug has released the firebuglite tool, which allows all browsers to support the console function and use firebug
Almost the same. See http://getfirebug.com/firebuglite
 
3. Why not use alert or self-written log directly?
When alert is used, the information can be displayed. Do you want to debug the program? The alert pop-up window will interrupt the program,
If you want to display information in the loop, you are exhausted by clicking close window. In addition, the alert Display object is always displayed as [object].
Although the self-written log can display some object information, many functions do not support the console well.
.
 
4. Console. Log (object [, object,...])
Console. log is the simplest method to output information to the console window. Multiple parameters are supported.
These parameters are displayed together. e. g:
 
 
The first parameter of the log method supports a replacement mode similar to the C-language printf string. For example, the preceding column can be written as follows:
 
Log supports the following replacement modes:
% S replace string
% D instead of Integer
% F replaces floating point value
% O replaces object
 
5. Console. debug, info, warn, error
The four methods are the same as the LOG method, but the displayed icons and text colors are different. e. g

6. Console. Assert (expression [, object,...])
The assert method is similar to the assert in unit test. When expression is false, the following information is output, e. g:
 
 
Note: The assert method is not supported in firebuglite, and chrome and firebug are supported.
 
7. Console. Clear ()
This method clears all information in the console.
 
8. Console. dir (object)
Print all attributes of the object in the form of a list. e. g:

IX. Console. dirxml (node)
Print the HTML code of the HTML element. e. g:
 
10 console. Trace ()
The trace method can view the call stack information of the current function, that is, how the current function is called. e. g:

Eleven console. Group (object [, object,...]), groupcollapsed, groupend
These three functions are used to group log and other output information for easy reading and viewing. Groupcollapsed
The method is the same as the group method, but the displayed group is collapsed by default. e. g:
 
Twelve console. Time (name)/console. timeend (name)
We often need to test the execution time of JS functions. We may write code in 1st statements and
The last statement minus the current time. This set of functions actually implements this function. Time (name) creates a new one based on the name
Timer. Timeend (name) stops the timer for a given name and displays the time. E. g:

Thirteen console. Profile (name)/console. profileend ()
This method is used to open the browser analyzer and analyze the JS execution between these functions,
 
Fourteen console. Count ([title])
The count method is used to count the number of times the current Code has not been executed. The title parameter can be output before the number of times
Additional titles for reading, e. g:
Note: firebuglite does not support the count method. The above code is printed 1000 times in chrome and is not as friendly as firebug.
 
15th console. Exception (error-object)
The exception method is used to print exception objects. Unlike log printing, parse t ion also prints
Stack information of the abnormal call, e. g:
Note: The exception method is unique to firebug, which is not supported by chrome and firebuglite.
 
Sixteen console. Table (data)
The table method displays data objects in tables, which are in the same array or format.
JSON objects are very useful. E

Note: Table only supports firebug and is available only after firebug1.6 +.
 
Summary:
Console is a very good tool to help us learn and debug Js. If you have never used it before, use it now. You
It can help you save a lot of development time.

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.