热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在铬中“打破财产变化”?-Howto“breakonpropertychange”inchrome?

Firebugforfirefoxhasanicefeature,calledBreakonpropertychange,whereIcanmarkanyprop

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.

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

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?

我试图在谷歌浏览器中实现相同,我无法在chrome调试器中找到该功能。我如何在谷歌浏览器中执行此操作?

6 个解决方案

#1


72  

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;
    }
});

#2


96  

Edit 2016.03: Object.observe is deprecated and removed in Chrome 50

编辑2016.03:在Chrome 50中弃用并删除了Object.observe

Edit 2014.05: Object.observe was added in Chrome 36

编辑2014.05:在Chrome 36中添加了Object.observe

Chrome 36 ships with native Object.observe implementation that can be leveraged here:

Chrome 36附带本机Object.observe实现,可在此处使用:

myObj = {a: 1, b: 2};
Object.observe(myObj, function (changes){
    console.log("Changes:");
    console.log(changes);
    debugger;
})
myObj.a = 42;

If you want it only temporarily, you should store callback in a variable and call Object.unobserve when done:

如果只是暂时需要它,则应将回调存储在变量中,并在完成后调用Object.unobserve:

myObj = {a: 1, b: 2};
func = function() {debugger;}
Object.observe(myObj, func);
myObj.a = 42;
Object.unobserve(myObj, func);
myObj.a = 84;

Note that when using Object.observe, you'll not be notified when the assignment didn't change anything, e.g. if you've written myObj.a = 1.

请注意,使用Object.observe时,如果分配没有改变任何内容,您将不会收到通知,例如如果你写了myObj.a = 1。

To see the call stack, you need to enable "async call stack" option in Dev Tools:

要查看调用堆栈,您需要在Dev Tools中启用“异步调用堆栈”选项:

chrome async call stack


Original answer (2012.07):

原始答案(2012.07):

A console.watch sketch as suggested by @katspaugh:

@katspaugh建议的console.watch草图:

cOnsole= console || {}; // just in case
console.watch = function(oObj, sProp) {
   sPrivateProp = "$_"+sProp+"_$"; // to minimize the name clash risk
   oObj[sPrivateProp] = oObj[sProp];

   // overwrite with accessor
   Object.defineProperty(oObj, sProp, {
       get: function () {
           return oObj[sPrivateProp];
       },

       set: function (value) {
           //console.log("setting " + sProp + " to " + value); 
           debugger; // sets breakpoint
           oObj[sPrivateProp] = value;
       }
   });
}

Invocation:

调用:

console.watch(obj, "someProp");

Compatibility:

兼容性:

  • In Chrome 20, you can paste it directly in Dev Tools at runtime!
  • 在Chrome 20中,您可以在运行时将其直接粘贴到Dev Tools中!
  • For completeness: in Firebug 1.10 (Firefox 14), you have to inject it in your website (e.g. via Fiddler if you can't edit the source manually); sadly, functions defined from Firebug don't seem to break on debugger (or is it a matter of configuration? please correct me then), but console.log works.
  • 为了完整性:在Firebug 1.10(Firefox 14)中,您必须将其注入您的网站(例如,如果您无法手动编辑源,请通过Fiddler);遗憾的是,从Firebug定义的函数似乎没有在调试器上中断(或者它是配置问题?请纠正我),但console.log工作。

Edit:

编辑:

Note that in Firefox, console.watch already exists, due to Firefox's non-standard Object.watch. Hence in Firefox, you can watch for changes natively:

请注意,在Firefox中,由于Firefox的非标准Object.watch,console.watch已经存在。因此,在Firefox中,您可以本地监视更改:

>>> var obj = { foo: 42 }
>>> obj.watch('foo', function() { console.log('changed') })
>>> obj.foo = 69
changed
69

However, this will be soon (late 2017) removed.

但是,这将很快(2017年底)删除。

#3


55  

There is a library for this: BreakOn()

有一个库:BreakOn()

If you add it to Chrome dev tools as a snippet (sources --> snippets --> right-click --> new --> paste this), you can use it anytime.

如果您将其作为代码段添加到Chrome开发工具(来源 - >代码段 - >右键单击 - >新建 - >粘贴此内容),您可以随时使用它。


To use it, open the dev-tools and run the snippet. Then to break when myObject.myProperty is changed, call this from the dev-console:

要使用它,请打开开发工具并运行代码段。然后在更改myObject.myProperty时中断,从dev-console调用它:

breakOn(myObject, 'myProperty');

You could also add the library to your project's debug-build so you don't need to call breakOn again every time you refresh the page.

您还可以将库添加到项目的debug-build中,这样每次刷新页面时都不需要再次调用breakOn。

#4


1  

This can also be done by using the new Proxy object whose purpose is exactly that: intercepting the reads and writes to the object that is wrapped by the Proxy. You simply wrap the object you would like to observe into a Proxy and use the new wrapped object instead of your original one.

这也可以通过使用新的Proxy对象来完成,该对象的目的恰恰是:拦截对Proxy包装的对象的读写。您只需将要观察的对象包装到代理中,然后使用新的包装对象而不是原始对象。

Example:

例:

const originalObject = {property: 'XXX', propertyToWatch: 'YYY'};
const watchedProp = 'propertyToWatch';
const handler = {
  set(target, key, value) {
    if (key === watchedProp) {
      debugger;
    }
    target[key] = value;
  }
};
const wrappedObject = new Proxy(originalObject, handler);

Now use wrappedObject where you would supply originalObject instead and examine the call stack on break.

现在使用wrappedObject来代替您提供originalObject并在break上检查调用堆栈。

#5


0  

function debugProperty(obj, propertyName) {
  // save in another property
  obj['_' + propertyName] = obj[propertyName];

  // overwrite with accessor
  Object.defineProperty(obj, propertyName, {
    get: function() {
      return obj['_' + propertyName];
    },

    set: function(value) {
      debugger; // sets breakpoint
      obj['_' + propertyName] = value;
    }
  });
}

#6


0  

Chrome has this feature built-in in latest versions https://developers.google.com/web/updates/2015/05/view-and-change-your-dom-breakpoints.

Chrome在最新版本https://developers.google.com/web/updates/2015/05/view-and-change-your-dom-breakpoints中内置了此功能。

So no more needs for custom libraries and solutions, just right click on DOM element in the inspector and choose 'Break on' -> 'attribute modifications' and that's it.

因此,不再需要自定义库和解决方案,只需右键单击检查器中的DOM元素并选择“Break on” - >“属性修改”即可。


推荐阅读
  • ImgettingabugwithInternetExplorer.Theiframedoesnthavecorrectsizeanditisdisplayedo ... [详细]
  • 本文整理了常用的CSS属性及用法,包括背景属性、边框属性、尺寸属性、可伸缩框属性、字体属性和文本属性等,方便开发者查阅和使用。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • 本文介绍了网页播放视频的三种实现方式,分别是使用html5的video标签、使用flash来播放以及使用object标签。其中,推荐使用html5的video标签来简单播放视频,但有些老的浏览器不支持html5。另外,还可以使用flash来播放视频,需要使用object标签。 ... [详细]
  • 本文概述了JNI的原理以及常用方法。JNI提供了一种Java字节码调用C/C++的解决方案,但引用类型不能直接在Native层使用,需要进行类型转化。多维数组(包括二维数组)都是引用类型,需要使用jobjectArray类型来存取其值。此外,由于Java支持函数重载,根据函数名无法找到对应的JNI函数,因此介绍了JNI函数签名信息的解决方案。 ... [详细]
  • this prototype 闭包 总结
    this对象整理下思路:一般用到this中的情景:1.构造方法中functionA(){this.nameyinshen;}varanewA() ... [详细]
  • Whyusingstringsaskeysofarray,consoleisshowingthatarraywithoutthesedeclaredvaluesand ... [详细]
  • JavaScript概述1.JavaScript定义JavaScript是Netscape公司开发的一种基于对象和事件驱动的脚本语言。它是弱类型语言,只能由浏览器解释执行。其中:脚本语言:解释运行( ... [详细]
  • JavaScript与DOM(上)——也适用于新手 – 深入理解JavaScript系列 23
    本文是《JavaScript深度解析》系列文章第23篇(共51篇)文档对象模 ... [详细]
  • 高仿CSDN社区树形图 .
    一直感觉CSDN社区的树形结构特别的人性化,直观化。最近做系统的时候需要用到这个树形结构,于是模仿CSDN的树形结构做了一个自己的树形结构, ... [详细]
  • IsapiCache组件 - 自动为网站生成…
    sapiCache是一款自动为网站生成静态页面的IIS组件(ISAPI筛选器),静态页面可以有效的加快网站访问速度,大大减轻服务器负担。工作原理是组件把服务器返回给客户端的网页源码保存起来,生成静态文 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • javascript如何判断值是否为undefined
    这篇文章主要介绍“javascript如何判断值是否为undefined”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“ja ... [详细]
  • 我试图制作一个进度条,如果还有更多内容仍被隐藏,则显示箭头。为了更清楚,我做 ... [详细]
author-avatar
cocoa果果_263
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有