Turn from:Nine console commands to make JS debugging easierI. Commands to display information
123456789101112131415 |
<!DOCTYPE html>
<title>常用console命令</title>
<meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8"
/>
<body>
<script type=
"text/javascript"
>
console.log(
‘hello‘
);
console.info(
‘信息‘
);
console.error(
‘错误‘
);
console.warn(
‘警告‘
);
</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):
placeholder |
function |
%s |
String |
%d or%i |
Integer |
%f |
Floating point number |
%o |
Expandable DOM |
%O |
List the properties of the DOM |
%c |
Format a string based on the provided CSS style |
123 |
<script type= "text/javascript" > console.log( "%d年%d月%d日" ,2011,3,26); </script> |
Effect:
%o,%o are used to output object objects, the normal object object, the two are no different, but the print DOM node is not the same:
12345 |
// 格式成可展开的的DOM,像在开发者工具Element面板那样可展开 console.log( ‘%o‘ ,document.body.firstElementChild); // 像JS对象那样访问DOM元素,可查看DOM元素的属性 // 等同于console.dir(document.body.firstElementChild) console.log( ‘%O‘ ,document.body.firstElementChild); |
The%c placeholder is the most common. When using the%c placeholder, the corresponding argument must be a CSS statement that is used to render the output CSS. There are two common types of output: Text style, picture output.
Text output
12 |
console.log( "%cHello world,欢迎您!" , "color: red; font-size: 20px" ); //输出红色的、20px大小的字符串:Hello world,欢迎您! |
In addition to ordinary text, you can also output the same characters as the console panel. These character paintings can be generated online:
- Picascii
- Mg2txt
- Ascii Generator
Approximate method: Use the online tool to generate the character draw, then copy it into sublime, remove the line break at the beginning of each line, and replace it with \ n. Finally, there is only one line of code, that is, guaranteed no line breaks, and finally dropped into the Console.log ("") code, of course, you can add a more cool effect with%c (the console output is not wrapped by default).
Picture output
Because the console cannot define an IMG, it is replaced with a background image. In addition, the console does not support width and height, use spaces and font-size instead, and can also use padding and line-height instead of width height.
Don't want to be so troublesome, can try console-image this plugin.
Iii. Grouping of information
12345678910111213141516171819 |
<!DOCTYPE html>
<title>常用console命令</title>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
<body>
<script type=
"text/javascript"
>
console.group(
"第一组信息"
);
console.log(
"第一组第一条:我的博客(http://www.ido321.com)"
);
console.log(
"第一组第二条:CSDN(http://blog.csdn.net/u011043843)"
);
console.groupEnd();
console.group(
"第二组信息"
);
console.log(
"第二组第一条:程序爱好者QQ群: 259280570"
);
console.log(
"第二组第二条:欢迎你加入"
);
console.groupEnd();
</script>
</body>
|
Effect:
Iv. Viewing the information of an object
Console.dir () can display all properties and methods of an object.
12345678 |
<script type=
"text/javascript"
>
var
info = {
blog:
"http://www.ido321.com"
,
QQGroup:259280570,
message:
"程序爱好者欢迎你的加入"
};
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.
1234567891011121314151617 |
<!DOCTYPE html>
<title>常用console命令</title>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
<body>
<div id=
"info"
>
<p>程序爱好者:259280570,欢迎你的加入</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.
123456 |
<script type= "Text/javascript" > &NBSP;&NBSP;&NBSP;&NBSP; var result = 1; &NBSP;&NBSP;&NBSP;&NBSP; Console.assert (result); &NBSP;&NBSP;&NBSP;&NBSP; var year =; &NBSP;&NBSP;&NBSP;&NBSP; 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.
1234567891011 |
<script type=
"text/javascript"
>
/*函数是如何被调用的,在其中加入console.trace()方法就可以了*/
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, chronograph function
Console.time () and Console.timeend () are used to display the elapsed time of the code.
1234567 |
<script type=
"text/javascript"
>
console.time(
"控制台计时器一"
);
for
(
var
i=0;i<1000;i++){
for
(
var
j=0;j<1000;j++){}
}
console.timeEnd(
"控制台计时器一"
);
</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 ().
123456789101112131415161718192021 |
<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(
‘性能分析器‘
);
All();
console.profileEnd();
</script>
|
Output
[Turn] Nine console commands, make JS debugging easier