Is there a way to “watch” a variable in google chrome?

强颜欢笑 提交于 2019-12-12 20:36:40

问题


Basically, I want to add a breakpoint every time a given closure variable is changed. Is there any way to do this?


回答1:


I don't think there's currently a way to directly watch variables, but if you can put the closure variable in an object, then you can use Object.observe() to observe that object for changes. (Object.observe can only observe objects)
This requires you to have Experimental Javascript enabled - chrome://flags/#enable-javascript-harmony.

(function(){
  var holder = { 
    watchedVariable: "something"
  };

  Object.observe(holder, function (changes) {
    // returns an array of objects(changes)

    if ( changes[0].name === "watchedVariable" ) {
      debugger;
    }

  });

})()


来源:https://stackoverflow.com/questions/22978549/is-there-a-way-to-watch-a-variable-in-google-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!