Console objects can be accessed in any global object such as Window, WorkerGlobalScope and special definitions provided through the properties workbench. It is defined by the browser as Window.Console, and can also be called by a simple Console. The Console object provides access to the browser console (such as Firefox's Web Console). The way it works on different browsers is different, but here are some interface features provided by Metropolis.
The most commonly used method is Console.log()
, which is to output content on the console. When I first started learning front-end, I saw that everyone was using Console.log()
, and I almost never saw other uses of Console
. Is Console
really No other usage? After checking it, I found that Console
is still very powerful. As for why I rarely see people using it, it may be because they have deleted it after using it. Record other uses of Console
here.
Note: Because the Console object provides access to the browser console, the support and representation in different browsers may be different, but only our developers can understand the debugging content. Look, just make sure that the development environment can use these methods. The following demonstrations are all the effects above Chrome
.
Classification output
Output of different categories of information
console.log('文字信息'); console.info('提示信息'); console.warn('警告信息'); console.error('错误信息');
Group output
Use Console.group()
and Console.groupEnd()
wrap the group content.
You can also use Console.groupCollapsed()
instead of Console.group()
to generate collapsed groups.
console.group('第一个组'); console.log("1-1"); console.log("1-2"); console.log("1-3"); console.groupEnd(); console.group('第二个组'); console.log("2-1"); console.log("2-2"); console.log("2-3"); console.groupEnd();
Console.group()
Can also be used nested
console.group('第一个组'); console.group("1-1"); console.group("1-1-1"); console.log('内容'); console.groupEnd(); console.groupEnd(); console.group("1-2"); console.log('内容'); console.log('内容'); console.log('内容'); console.groupEnd(); console.groupEnd(); console.groupCollapsed('第二个组'); console.group("2-1"); console.groupEnd(); console.group("2-2"); console.groupEnd(); console.groupEnd();
Table output
Use console.table()
The passed in object or array can be output in table form. Suitable for neatly arranged elements
var Obj = { Obj1: { a: "aaa", b: "bbb", c: "ccc" }, Obj2: { a: "aaa", b: "bbb", c: "ccc" }, Obj3: { a: "aaa", b: "bbb", c: "ccc" }, Obj4: { a: "aaa", b: "bbb", c: "ccc" } } console.table(Obj); var Arr = [ ["aa","bb","cc"], ["dd","ee","ff"], ["gg","hh","ii"], ] console.table(Arr);
View objects
UseConsole.dir()
Display an object All properties and methods
In ChromeConsole.dir()
and Console.log()
have the same effect
var CodeDeer = { nema: 'CodeDeer', blog: 'www.xluos.com', } console.log("console.dir(CodeDeer)"); console.dir(CodeDeer); console.log("console.log(CodeDeer)"); console.log(CodeDeer);
View nodes
Use Console.dirxml()
Display all properties and methods of an object
In ChromeConsole.dirxml()
The effect is the same as Console.log()
Node information of Baidu homepage logo
Conditional output
Use console.assert()
to perform conditional output.
When the first parameter or return value is true, no content is output
When the first parameter or return value is false , output the following content and throw an exception
console.assert(true, "你永远看不见我"); console.assert((function() { return true;})(), "你永远看不见我"); console.assert(false, "你看得见我"); console.assert((function() { return false;})(), "你看得见我");
Console.count()Output content and the number of times it was called
(function () { for(var i = 0; i <p><span class="img-wrap"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/054/025/41021841dae6dd478f110b5b1f520579-7.png?x-oss-process=image/resize,p_40" class="lazy" alt="Javascript debugging commands are more than Console.log()" ></span></p>Trace the call stack<h1></h1>Use<p> Console.trace()<code> is used to track the process of function being called. In complex projects, there are many calling processes. Use this command to help you clarify. </code></p><pre class="brush:php;toolbar:false">function add(a, b) { console.trace("Add function"); return a + b; } function add3(a, b) { return add2(a, b); } function add2(a, b) { return add1(a, b); } function add1(a, b) { return add(a, b); } var x = add3(1, 1);
Console.time()and
Console.timeEnd( )Wrap the code fragment that needs timing and output the event that runs this code.
The parameters in Console.time()
serve as the identifier of the timer and are unique.
Parameters in Console.timeEnd()
to end the timer identified by this and return the running time in milliseconds.
- Up to 10,000 timers can be run simultaneously.
-
##console.time("Chrome中循环1000次的时间"); for(var i = 0; i
##Performance Analysis
Console.profile()
andConsole.profile() to conduct performance analysis and check the time consumed by each part of the code, but I do not have it in the debugging tool that comes with Chrome. Find out where to view the analysis reports generated by both methods. Other debugging tools may be required.
有趣的Console.log()
最后再来介绍一下强大的Console.log()
,这个方法有很多的用法(其他输出方法的用法,如error()等,可以参照log()使用)。
一、提示输出
可以再输出的对象、变量前加上提示信息,增加辨识度
var ans = 12345; console.log("这是临时变量ans的值:",ans);
二、格式化输出
占位符 | 含义 |
---|---|
%s | 字符串输出 |
%d or %i | 整数输出 |
%f | 浮点数输出 |
%o | 打印javascript对象,可以是整数、字符串以及JSON数据 |
样例:
var arr = ["小明", "小红"]; console.log("欢迎%s和%s两位新同学",arr[0],arr[1]); console.log("圆周率整数部分:%d,带上小数是:%f",3.1415,3.1415);
三、自定义样式
使用%c
为打印内容定义样式,再输出信息前加上%c
,后面写上标准的css样式,就可以为输出的信息添加样式了
console.log("%cMy stylish message", "color: red; font-style: italic"); console.log("%c3D Text", " text-shadow: 0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);font-size:5em"); console.log('%cRainbow Text ', 'background-image:-webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );color:transparent;-webkit-background-clip: text;font-size:5em;'); console.log('%cMy name is classicemi.', 'color: #fff; background: #f40; font-size: 24px;');
总结
Console的用法很多,有些再调试过程中非常实用,可以节省很多时间。当然我知道debug还是用断点调试的方法比较好,但是小问题用“printf大法”也是很好用的(滑稽脸)。
相关推荐:
The above is the detailed content of Javascript debugging commands are more than Console.log(). For more information, please follow other related articles on the PHP Chinese website!

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment