Skip to content

Instantly share code, notes, and snippets.

@vaidd4
Last active March 22, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaidd4/79e2b4b9395c11ec6f276c2b89ec2dad to your computer and use it in GitHub Desktop.
Save vaidd4/79e2b4b9395c11ec6f276c2b89ec2dad to your computer and use it in GitHub Desktop.
A console.watch() implementation
/**
* 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