Created
June 24, 2015 17:00
-
-
Save mping/b50e4fe6a98d9a05ee3f to your computer and use it in GitHub Desktop.
Object.defineProperty and debugger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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