AlexZ
AlexZ

Reputation: 12073

Is there a way to "watch" a variable in google chrome?

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

Upvotes: 2

Views: 4250

Answers (1)

user3071008
user3071008

Reputation:

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

  });

})()

Upvotes: 4

Related Questions