Last active
March 22, 2018 13:45
-
-
Save vaidd4/79e2b4b9395c11ec6f276c2b89ec2dad to your computer and use it in GitHub Desktop.
A console.watch() implementation
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
/** | |
* Watch for object properties | |
* you can log on each property change and even add a breakpoint | |
*/ | |
function consoleWatch (oObj, sProp) { | |
let sPrivateProp = `_${sProp}_$` | |
oObj[sPrivateProp] = oObj[sProp] | |
Object.defineProperty(oObj, sProp, { | |
get: () => { return oObj[sPrivateProp] }, | |
set: value => { | |
console.log(`DEBUG '${sProp}':`, value) | |
// debugger; // uncomment to add breakpoint on value changes | |
oObj[sPrivateProp] = value | |
} | |
}) | |
} | |
/** | |
* Usage | |
*/ | |
consoleWatch(document, 'property') | |
/** | |
* [JS Standard](https://standardjs.com/demo.html?gist=79e2b4b9395c11ec6f276c2b89ec2dad) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment