JS Debug Tool Console command

Source: Internet
Author: User

Console: Console log:n record, logbook v logging voyage

I. Commands to display information

Copy CodeThe code is as follows:
<! DOCTYPE html>
<title> Common Console Commands </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
<script type= "Text/javascript" >
Console.log (' Hello ');
Console.info (' information ');
Console.error (' error ');
Console.warn (' warning ');
</script>
</body>

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)

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
Console.log ("%d years%d months%d days", 2011,3,26);
</script>

Effect:

Iii. Grouping of information

Copy CodeThe code is as follows:
<! DOCTYPE html>
<title> Common Console Commands </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
<script type= "Text/javascript" >
Console.group ("First set of Information");

Console.log ("First Group first: my xx (http://www.jb51.net)");

Console.log ("First Group II: XXX (http://jb51.net)");

Console.groupend ();

Console.group ("Second set of Information");

Console.log ("The second group first: program Enthusiasts QQ Group: 80535344");

Console.log ("Second group II: welcome you to join");

Console.groupend ();
</script>
</body>

Effect:


Iv. Viewing the information of an object

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

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
var info = {
Blog: " http://www.jb51.net",
qqgroup:80535344,
Message: "Program enthusiasts welcome you to join"
};
Console.dir (info);
</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.

Copy CodeThe code is as follows:
<! DOCTYPE html>
<title> Common Console Commands </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
<div id= "Info" >
Www.ido321.com>
<p> Program Enthusiasts: 259280570, you are welcome to join </p>
</div>
<script type= "Text/javascript" >
var info = document.getElementById (' info ');
Console.dirxml (info);
</script>
</body>

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.

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
var result = 1;
Console.assert (result);
var year = 2014;
Console.assert (Year = = 2018);
</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.

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
/* How the function is called, in which the Console.trace () method is available */
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);}
</script>

Console output Information:


Eight, chronograph function

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

Copy CodeThe code is as follows:
<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 analysis of the various parts of the running time, to find out the bottleneck, the method used is Console.profile ().

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
function All () {
Alert (11);
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>

Description, LZ test, in all () without alert, control bar no output, plus, there is a performance analysis table, temporarily unclear why, if you know, you can comment.

Console command line explanation
First of all, in IE, if the console is not turned on using consoles will error, but there is no relationship, open the developer tools Refresh all normal.
1, Console.log
We believe that one of the most commonly used console properties is console.log. His role is to spread the general message to the console. For example:
    1. var a=3;
    2. Console.log (a);
Copy CodeAfter running we can see a 3 printed on the console. This property also helps us to print out an object or function. Like what:
    1. function A () {
    2. var b=3;
    3. }
    4. var a1=new a ();
    5. Console.log (A1);
Copy CodeAt this point the console will print an instance of the A object, for Chrome, we can see the entire structure of the instance of a object, including the inheritance situation, for firebug, we can only see some of his properties, but for IE, if you print an instance, will only return [ Object,object]

2, Console.info
The purpose of this command is to return an informational message, which differs greatly from the Console.log, except that only the text will be printed in chrome, and the rest will display a blue exclamation point.

3.console.warn and Console.error
The purpose of these two commands is to help us output warning messages and error messages. When warning, the console will appear with a yellow exclamation point in addition to the output information, and a red fork when the error occurs.

The above is the console display information command. However, some people will ask, these commands are only output, what is the use of the specific? This is primarily a sort of processing of messages, for example, if we just want to test whether a function is running, then using Console.log is completely sufficient, but when we wish to throw some unknown error, we can use Console.error to return our own set of errors.

4. Placeholder Display
The biggest advantage of console display output is the support placeholder. However, the support is relatively small, currently supports characters (%s), integers (%d), floating point (%f), and objects (%o)

This is very much like printf in Java and C, and it's the same usage.
For example Console.log (%d,3), Console.log (%f,4.2323),
When we use%o, we return the internal condition of an object.
For example:
    1. <spanstyle= "line-height:1.5;" = "" >var a={}

    2. A.b=3
    3. A.c=5
    4. Console.log ("%o", a) </spanstyle= "line-height:>
Copy Code5. Group Display
Use Console.group and console.groupend (note case) to group the returned data so that we can sort the comparisons. For example:
    1. Console.group (' first group ')
    2. Console.log (3)
    3. Console.log (5)
    4. Console.log (7)
    5. Console.groupend ()

    6. Console.group (' second group ')
    7. Console.log (4)
    8. Console.log (6)
    9. Console.log (8)
    10. Console.groupend ()
Copy Codeappear in Chrome as:

But this command is not supported by our great IE browser


6, Console.dir
The purpose of this command is to print out all the properties within an object
For example:
    1. var a={}
    2. a.b=3;
    3. a.c=5;

    4. Console.dir (a)
Copy CodeSome of the hidden properties will be printed in chrome except for the B,C two variables we define.

Only print out b,c in Firebug

Only [Object,object] will be printed in IE.

7, Console.dirxml
This command our great IE still does not support, his role is to print out the book Dom document tree, for example, say:
  1. <body>
  2. <div id= "Tree" >
  3. <ul>
  4. <li>aaa</li>
  5. <li>bbb</li>
  6. <li>ccc</li>
  7. <li>ddd</li>
  8. </ul>

  9. </div>

  10. </body>

  11. <script>
  12. var Tree=document.getelementbyid ("Tree");

  13. Console.dirxml (tree)
  14. </script>
Copy CodeThat's the way it is in chrome.

It's basically the same in Firebug.

8, Console.trace
This command is used to keep track of a function's call trajectory, which is helpful if you add console.trace to the function you want to test, of course, our great IE still doesn't support it.

9, Console.assert
This command is used to determine whether an expression or variable is true
For example:
    1. var a=3;
    2. Console.assert (a==5);
Copy CodeBack in Chrome:

IE returns:

Firebug return:


unnamed ff.jpg (6.93 KB, download count:)

Console command line explanation



1, Console.log
We believe that one of the most commonly used console properties is console.log. His role is to spread the general message to the console. For example:
    1. var a=3;
    2. Console.log (a);
Copy CodeAfter running we can see a 3 printed on the console. This property also helps us to print out an object or function. Like what:
    1. function A () {
    2. var b=3;
    3. }
    4. var a1=new a ();
    5. Console.log (A1);
Copy CodeAt this point the console will print an instance of the A object, for Chrome, we can see the entire structure of the instance of a object, including the inheritance situation, for firebug, we can only see some of his properties, but for IE, if you print an instance, will only return [ Object,object]

2, Console.info
The purpose of this command is to return an informational message, which differs greatly from the Console.log, except that only the text will be printed in chrome, and the rest will display a blue exclamation point.

3.console.warn and Console.error
The purpose of these two commands is to help us output warning messages and error messages. When warning, the console will appear with a yellow exclamation point in addition to the output information, and a red fork when the error occurs.

The above is the console display information command. However, some people will ask, these commands are only output, what is the use of the specific? This is primarily a sort of processing of messages, for example, if we just want to test whether a function is running, then using Console.log is completely sufficient, but when we wish to throw some unknown error, we can use Console.error to return our own set of errors.

4. Placeholder Display
The biggest advantage of console display output is the support placeholder. However, the support is relatively small, currently supports characters (%s), integers (%d), floating point (%f), and objects (%o)

This is very much like printf in Java and C, and it's the same usage.
For example Console.log (%d,3), Console.log (%f,4.2323),
When we use%o, we return the internal condition of an object.
For example:
    1. <spanstyle= "line-height:1.5;" = "" >var a={}

    2. A.b=3
    3. A.c=5
    4. Console.log ("%o", a) </spanstyle= "line-height:>
Copy Code5. Group Display
Use Console.group and console.groupend (note case) to group the returned data so that we can sort the comparisons. For example:
    1. Console.group (' first group ')
    2. Console.log (3)
    3. Console.log (5)
    4. Console.log (7)
    5. Console.groupend ()

    6. Console.group (' second group ')
    7. Console.log (4)
    8. Console.log (6)
    9. Console.log (8)
    10. Console.groupend ()
Copy Codeappear in Chrome as:

But this command is not supported by our great IE browser


6, Console.dir
The purpose of this command is to print out all the properties within an object
For example:
    1. var a={}
    2. a.b=3;
    3. a.c=5;

    4. Console.dir (a)
Copy CodeSome of the hidden properties will be printed in chrome except for the B,C two variables we define.

Only print out b,c in Firebug

Only [Object,object] will be printed in IE.

7, Console.dirxml
This command our great IE still does not support, his role is to print out the book Dom document tree, for example, say:
  1. <body>
  2. <div id= "Tree" >
  3. <ul>
  4. <li>aaa</li>
  5. <li>bbb</li>
  6. <li>ccc</li>
  7. <li>ddd</li>
  8. </ul>

  9. </div>

  10. </body>

  11. <script>
  12. var Tree=document.getelementbyid ("Tree");

  13. Console.dirxml (tree)
  14. </script>
Copy CodeThat's the way it is in chrome.

It's basically the same in Firebug.

8, Console.trace
This command is used to keep track of a function's call trajectory, which is helpful if you add console.trace to the function you want to test, of course, our great IE still doesn't support it.

9, Console.assert
This command is used to determine whether an expression or variable is true
For example:
    1. var a=3;
    2. Console.assert (a==5);
Copy CodeBack in Chrome:

IE returns:

Firebug return:


unnamed ff.jpg (6.93 KB, download count:)

JS Debug Tool 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.