Deep analysis of the console commands in JavaScript _javascript tips

Source: Internet
Author: User
Tags assert

A command to display information

The code is as follows:

<! 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)

The code is as follows:

<script type= "Text/javascript" >
console.log ("%d years%d months%d days", 2016,8,20);
</script>

Effect:

Iii. Grouping of information

The code is as follows:

<! DOCTYPE html>
 
 

Effect:

Four, view the object's information

Console.dir () can display all the properties and methods of an object.
The code is as follows:

<script type= "Text/javascript" >
var info = {
blog: "https://segmentfault.com/u/learnme/",
GitHub: "https://github.com/specialCoder/", 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.
The code is as follows:

<! DOCTYPE html>
 
 

Effect:

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.
ASSERT (Assert) is a good feature to ensure program correctness. On browsers with debugging tools, this feature can be implemented by calling Console.assert ().

The code is as follows:

<script type= "Text/javascript" >
var result = 1;
Console.assert (result);
var year = 2014;
Console.assert (year===2018, "assertion failed");
</script>

In the Console.assert () statement, the first parameter is the result of an assert, which should normally be true, and the second parameter is an error message printed on the console when an error occurs.

1 is not 0 value, is true, and the second is false, the error message is displayed on the console

Show Results:

Seven, tracing function's call path

Console.trace () is used to track the call path of the function.
The code is as follows:

<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.
The code is as follows:

<script type= "Text/javascript" >
console.time ("Count 1000*1000");
for (Var i=0;i<1000;i++) {for
(var j=0;j<1000;j++) {}
}
console.timeend ("Count 1000*1000");
</script>

Run Time is 10.64ms

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 ().
The code is as follows:

<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>    

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

Related Article

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.