- Published on
11-对象&方法
- Authors
- Name
- Dendoink
- @Magiciaaaaaaaan
💡
问题 在我们调试 Javascript
的代码时, 对象
和 方法
作为经常被我们调试的对象,所以这里介绍关于 对象
和 方法
的调试技巧。
queryObjects
(对象查询)方法
1. class Person {
constructor(name, role) {
this.name = name;
this.role = role;
}
}
const jack_ma = new Person('Jack Ma', 'Dad')
const CEOS = [
new Person('Pony Ma', 'CEO'),
new Person('Richard LIU', 'CEO'),
]
假如我们有这样一段代码,我们在里面定义了一些对象。
在代码执行的 某个特定的时刻 + 特定的执行上下文 有哪些对象呢?
在 DevTools
里的 queryObjects
函数可以展示这些信息。

monitor
监控
2. 每当一个被 monitor
的方法执行时, console 控制台
会打印当期被调用的 函数名 以及 参数:
例子
function sum(x, y) {
return x + y;
}
monitor(sum);

我们可以少写很多 console.logs
!
💡
敲黑板 执行 unmonitor()
可以停止对某一个函数的监控
monitorEvents
事件监控
3. 在上文中,我们讨论了用 monitor
方法来监听函数,其实还可以使用名为 monitorEvents
的方法,对 events
做一样的事情:
当指定对象上发生指定事件之一时,事件对象将记录到控制台。
不光是单个事件,甚至是事件数组或事件集合的一般事件类型:
监视窗口对象上的所有调整大小事件:
monitorEvents(window, "resize");

你可以同时对 resize
还有 scroll
事件进行监听:
monitorEvents(window, ["resize", "scroll"])
指定一种事件类型进行监听:
类型 | 事件 |
---|---|
mouse | "mousedown", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout", "mousewheel" |
key | "keydown", "keyup", "keypress", "textInput" |
touch | "touchstart", "touchmove", "touchend", "touchcancel" |
control | "resize", "scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset" |
给当前选中的输入框,加上键盘事件的监听:
monitorEvents($0, "key");

Related series
00-写在前面
March 2, 2021
01-Chrome 版本
March 2, 2021
02-复制 & 保存
March 2, 2021
03-快捷键和通用技巧
March 2, 2021
04-使用 Command
March 2, 2021
05-代码块的使用
March 2, 2021
06-console 中的 $
March 2, 2021
07-console.log 的 "bug" ?
March 2, 2021
08-异步的 console
March 2, 2021
09-Ninja console.log
March 2, 2021
10-自定义格式转换器
March 2, 2021
11-对象&方法
March 2, 2021
12-console.log 并不简单
March 2, 2021
13-打造一个终极 log
March 2, 2021
14-打印时间戳
March 2, 2021
15-直接把 `log` 作为回调函数
March 2, 2021
16-初窥 Network
March 2, 2021
17-Network 深入
March 2, 2021
18-Network 细节
March 2, 2021
19-元素面板的基本操作
March 2, 2021
20-元素面板调试技巧
March 2, 2021
21-元素面板里的编辑器
March 2, 2021
22-颜色选择器
March 2, 2021
23-Drawer 使用技巧
March 2, 2021
24-Drawer 动画调试
March 2, 2021
25-Drawer 性能优化
March 2, 2021
26-Workspace 使用技巧
March 2, 2021
27-自动美化输出
March 2, 2021
28-CSS 概览
March 2, 2021
29-Custom UI Themes
March 2, 2021
30-Source diff
March 2, 2021