Console 中的一些函数
- monitor: 使用monitor函数来监控函数,如:
- unmonitor: 停止监控指定函数
- copy: copy 函数可以把变量的值复制到剪贴板上
- inspect: inspect函数可以让你控制台跳到需要查看的DOM对象上
- debug: 调用指定函数时,将触发调试程序,并在Sources面板上使函数内部中断。debug(jQuery.Animation)
- undebug: 停止函数中断
- getEventListeners(object): 用来查看对象上注册的事件监听器,返回一个对象, 包含每个注册的事件类型数组
- monitorEvents(object[,events]): 监控相关事件。 当指定对象上发生一个指定事件时,将Event对象输出到控制台
- unmonitorEvents(object[,events]): 可停止对指定对象的指定事件监控。
- queryObjects: 查询存在内存中的类型实例
下面是我日常使用的调试函数
-
observeFocus()
: 观测焦点变动,用于定位改变焦点的源代码(() => { window.focusStack = []; window.observeFocus = () => { if(window.unobserveFocus) { window.unobserveFocus(); } const onfocusOptions = { capture: true }; document.addEventListener('focus', onfocus, onfocusOptions); function onfocus() { // debugger; console.trace(document.activeElement); const last = window.focusStack[focusStack.length - 1]; if(last !== document.activeElement) { window.focusStack.push(document.activeElement); } } window.unobserveFocus = () => { document.removeEventListener('focus', onfocus, onfocusOptions) }; }; })()
-
observePropertiesChange(object, properties, breakpoint = false)
: 对象属性值变更时输出堆栈信息或打断点window.observePropertiesChange = (object, properties, bp = false) => { c onst originalDescriptors = properties.map(it => { return [it, Object.getOwnPropertyDescriptor(object, it)]; }); Object.defineProperties(object, properties.reduce((desc, key) => { let value = object[key]; desc[key] = { get: () => { return value; }, set: newValue => { if(bp) { debugger; } else { console.trace('value changed', { object, key, value, newValue }); } value = newValue; } }; return desc; },{})); return ()=> { originalDescriptors.forEach(([key, desc]) => { Object.defineProperty(key, desc); }); }; }
-
阻止事件冒泡或阻止默认行为时输出堆栈信息或打断点
window.detectPreventDefault = function(type, bp = false){ const method = 'preventDefault'; switch(type) { case 'key': return debugOn(KeyboardEvent.prototype, method, bp); case 'mouse': return debugOn(MouseEvent.prototype, method, bp); case 'touch': return debugOn(TouchEvent.prototype, method, bp); case 'pointer': return debugOn(PointerEvent.prototype, method, bp); } } window.detectStopPropagation = function(type, bp = false){ const method = 'stopPropagation'; switch(type) { case 'key': return debugOn(KeyboardEvent.prototype, method, bp); case 'mouse': return debugOn(MouseEvent.prototype, method, bp); case 'touch': return debugOn(TouchEvent.prototype, method, bp); case 'pointer': return debugOn(PointerEvent.prototype, method, bp); } } window.debugOn = function(target, name, bp) { const originDescriptor = Object.getOwnPropertyDescriptor(target, name); const origin = target[name]; Object.defineProperty(target, name, { value: function(...args){ if(bp) { debugger; } else { console.trace(target, name); } return origin.apply(this, args); }, enumerable: true, writable: false, configurable: true }); return () => { Object.defineProperty(target, name, originDescriptor) }; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗