Skip to content

Instantly share code, notes, and snippets.

@mping
Created June 24, 2015 17:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mping/b50e4fe6a98d9a05ee3f to your computer and use it in GitHub Desktop.
Save mping/b50e4fe6a98d9a05ee3f to your computer and use it in GitHub Desktop.
Object.defineProperty and debugger
// Nice trick when you don't know where a change comes from
// rewrites a property, and sets a debugger when the property changes
console = console || {}; // just in case
console.watch = function(oObj, sProp) {
var 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) {
debugger; // <<<<<<<<<<< sets breakpoint, do whatever you want
oObj[sPrivateProp] = value;
}
});
}
// ...
$scope.myObj = {value: 1}
console.watch($scope.myObj, 'value')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment