[英]Google Chrome Developer Tools - Globally Watching a Variable
I need to trace the code that changes the property of an object. 我需要跟踪更改对象属性的代码。 Breakpoints in Google Chrome DevTools are set upon line numbers;
Google Chrome DevTools中的断点是根据行号设置的; but in this particular scenario I don't know the code that manipulates the object in application flow, hence can't apply watch expressions over lines.
但是在这种特殊情况下,我不知道在应用程序流程中操纵对象的代码,因此无法在行上应用监视表达式。
Is there a way that I can watch a variable in application scope regardless of lines of code? 有没有一种方法可以不管代码行如何在应用程序范围内监视变量?
Please not that I need to find the location in source code where an objects property gets changed not "when" or "what" a change applied. 请不要让我需要在源代码中找到对象属性更改的位置,而不是“何时”或“什么”应用更改。
The Object.prototype.watch()
provides a way to have a callback function executed when a property of an object changes. Object.prototype.watch()
提供了一种在对象的属性更改时执行回调函数的方法。
From the MDN documentation : 从MDN文档中 :
var o = { p: 1 };
o.watch('p', function (id, oldval, newval) {
console.log('o.' + id + ' changed from ' + oldval + ' to ' + newval);
return newval;
});
o.p = 2;
o.p = 3;
delete o.p;
o.p = 4;
o.unwatch('p');
o.p = 5;
outputs: 输出:
o.p changed from 1 to 2
o.p changed from 2 to 3
o.p changed from undefined to 4
Also, ECMAScript 7 will provide a more advanced Object.observe()
function: see https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/observe 此外,ECMAScript 7将提供更高级的
Object.observe()
函数:请参阅https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/observe
You can define property accessor functions and set breakpoints in them. 您可以定义属性访问器函数并在其中设置断点。 See defineSetter for more details.
有关更多详细信息,请参见defineSetter 。
var o = {}; Object.defineProperty(o, 'value', { set: function(val) { this._value = val; console.log(val); } }); o.value = 5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.