Skip to content

Instantly share code, notes, and snippets.

@MD4
Last active June 14, 2017 13:52
Show Gist options
  • Save MD4/aef3472fc9d45367e4cadcb71db7fb81 to your computer and use it in GitHub Desktop.
Save MD4/aef3472fc9d45367e4cadcb71db7fb81 to your computer and use it in GitHub Desktop.
Refacto tool which highlight every use of a variable on an object.
/**
* Example:
* observe('myProp').on(myObject);
*/
global.observe = (varName) => ({
on: (object) => {
let value = object[varName];
Object.defineProperty(
object,
varName,
{
enumerable: true,
configurable: true,
get: () => console.trace(
`%cGET%c ${varName}`,
'color: green; font-weight: bold;',
'font-weight: bold;'
) || value,
set: (newValue) => console.trace(
`%cSET%c ${varName}`,
'color: red; font-weight: bold;',
'font-weight: bold;'
) || (value = newValue)
}
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment