财产变化的断点 [英] Breakpoint on property change

查看:146
本文介绍了财产变化的断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebug for Firefox有一个很好的功能,称为Break on property change,我可以在其中标记任何对象的任何属性,它将在更改之前停止JavaScript执行。

Firebug for Firefox has a nice feature, called "Break on property change", where I can mark any property of any object, and it will stop JavaScript execution right before the change.

我正在尝试在Google Chrome中实现相同功能,但我无法在Chrome调试器中找到该功能。如何在Google Chrome中执行此操作?

I'm trying to achieve the same in Google Chrome, and I can't find the function in Chrome debugger. How do I do this in Google Chrome?

推荐答案

如果您不介意搞乱来源,可以重新定义带有访问者的财产。

If you don't mind messing around with the source, you could redefine the property with an accessor.

// original object
var obj = {
    someProp: 10
};

// save in another property
obj._someProp = obj.someProp;

// overwrite with accessor
Object.defineProperty(obj, 'someProp', {
    get: function () {
        return obj._someProp;
    },

    set: function (value) {
        debugger; // sets breakpoint
        obj._someProp = value;
    }
});

这篇关于财产变化的断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆