posts - 21,comments - 0,views - 13619

Console 中的一些函数

  1. monitor: 使用monitor函数来监控函数,如:
  2. unmonitor: 停止监控指定函数
  3. copy: copy 函数可以把变量的值复制到剪贴板上
  4. inspect: inspect函数可以让你控制台跳到需要查看的DOM对象上
  5. debug: 调用指定函数时,将触发调试程序,并在Sources面板上使函数内部中断。debug(jQuery.Animation)
  6. undebug: 停止函数中断
  7. getEventListeners(object): 用来查看对象上注册的事件监听器,返回一个对象, 包含每个注册的事件类型数组
  8. monitorEvents(object[,events]): 监控相关事件。 当指定对象上发生一个指定事件时,将Event对象输出到控制台
  9. unmonitorEvents(object[,events]): 可停止对指定对象的指定事件监控。
  10. queryObjects: 查询存在内存中的类型实例

下面是我日常使用的调试函数

  1. 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)
        };
    };
    })()
    
  2. 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);
            });
        };
    }
    
  3. 阻止事件冒泡或阻止默认行为时输出堆栈信息或打断点

    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)
        };
    }
    
posted on   y1j2x34  阅读(19)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示