You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.time("Array initialize");
var array= new Array(1000000);
for (var i = array.length - 1; i >= 0; i--) {
array[i] = new Object();
};
console.timeEnd("Array initialize");
结果:
console.trace可追踪执行路径
function foo() {
function bar() {
console.trace();
}
bar();
}
foo();
The output in the console looks something like this:
Formats the value as an expandable DOM element. As seen in the Elements panel
%O
Formats the value as an expandable JavaScript object
%c
Applies CSS style rules to the output string as specified by the second parameter
格式化输出CSS样式
// 控制台可直接输出带有样式的内容
console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large");
格式化DOM元素为对象:
By default, DOM elements are logged into the console as representation of their HTML, but sometimes you want to access the DOM element as JavaScript object and inspect its properties. You can use the %o string specifier to do that (see above), or use console.dir to achieve the same:
Near the top of the sidebar is the Call Stack section. When the code is paused at a breakpoint, the call stack shows the execution path, in reverse chronological order, that brought the code to that breakpoint. This is helpful in understanding not just where the execution is now, but how it got there, an important factor in debugging.
An initial onclick event at line 50 in the index.html file called thesetone() function at line 18 in the dgjs.js JavaScript file, which then called the setall() function at line 4 in the same file, where execution is paused at the current breakpoint.
Chrome调试相关
因图片无法上传,原文传到简书
Chrome调试工具是日常项目中常用到的,但面对
Command + Shift + i
(window:F12
)开启调试面板,总有些不被知晓的功能被遗漏。本着工欲善其事,必先利其器的想法,扒点历史存货,也从官网系统学习一下Chrome较为完善的功能.Elements Panel
所见即所得,主要用于显示编辑
html
及style
样式DOM Inspect:
inspect
Enter
,then pressTab
可快速选择属性,进行编辑DOM样式修改:
html
双击修改,style
单击修改,tab
orenter
确认修改演示地址:
历史修改查看
Set DOM breakpoints
按照
DOM Inspect
提示选中元素后,可右键设置DOM breakpoints
:Subtree Modifications
: 加在父级上的断点,用来监听子元素的内容增删Attribute Modifications
:加在元素上的断点,用来监听元素的属性变化Node Removal
:加在元素上的断点,用来监听元素是否被删除断点事件触发,会有如下提示
Console Panel
Shortcut: Press
Ctrl+Shift+J
(Windows / Linux) orCmd+Opt+J
(Mac).Console:输出控制台,前端调试后花园,内容输出显示,或者直接执行
javascript
代码Write Console
javascript
文件中执行console.log('some function or string')
控制台直接输入命令执行
Console API
console.group
:内容输出成组效果如下:
console.error()
:输出错误console.warn()
:输出警告console.assert
:输出断言,不影响后续的函数执行,断言可以viewexception stack trace.console.table
:表格[官方示例](https://developers.google.com/web/tools/chrome-devtools/debug/console/structured-data?hl=en)
console.time(arg)
,console.timeEnd(arg)
:查看函数执行时间。结果:
console.trace
可追踪执行路径The output in the console looks something like this:
完整Console文档在这里:Console API Reference
Console Format
基本Format.如上,内容输出可进行格式化输出.Console支持的输出列表:
格式化输出CSS样式
格式化DOM元素为对象:
Print stack traces
以上
console.trace
可实现路径追踪,在报错中,我们可以通过Error
输出打印Error.stack
:error 的stack
属性可以输出执行路径
Tips:
clear()
可快速实现清屏console.clear()
,解除控制台各种问题Source Panel
Source:顾名思义,加载的资源这里都能找得到。此部分主要介绍断点。
Set Breakpoint
js
文件中设置在
js
文件中需要执行断点操作的code line前加上debugger
Breakpoint conditional断点执行条件
右键设置的断点,选择
Edit breakpoint
输入执行断点的条件Breakpoint Step
Call Stack
可以通过勾选
Async
实现异步查找事件依赖关系,方便debug
。官方建议避免使用匿名函数。可以通过右键在对应的脚本(如第三方)文件中设置
blackbox
关进小黑屋,防止迷惑debug
.官方是通过tools
的setting
进行全局设置。Data manipulation更改执行数值
暂停break,控制台输出需要变更的值,强制更改值即可继续按照所需执行.
针对Workspaces 和 Sourcemaps开启的权限,见下。
Set Up Persistence with DevTools Workspaces
Set Up CSS & JS Preprocessors
参考链接
The text was updated successfully, but these errors were encountered: