Skip to content

Instantly share code, notes, and snippets.

@rjharmon
Forked from dmethvin/gist:1676346
Last active August 29, 2015 14:06
Show Gist options
  • Save rjharmon/789bfaa8dc5ab91bd515 to your computer and use it in GitHub Desktop.
Save rjharmon/789bfaa8dc5ab91bd515 to your computer and use it in GitHub Desktop.
function debugAccess(obj, prop, debugGet){
var origValue = obj[prop];
var newKey = "__debug_access_"+prop;
obj[newKey] = origValue;
Object.defineProperty(obj, prop, {
get: function () {
if (this !== obj && this.hasOwnProperty(newKey)) {
console.log("Inherited object gets its existing COPY of '", prop, "' in ", newKey);
return this[newKey];
}
if (this === obj) {
console.log("Getter: '", prop, "' directly from myself:", obj)
} else {
console.log("Getter: '", prop, "' inherited from ", obj)
}
if ( debugGet ) debugger;
return this[newKey];
},
set: function(val) {
if (this !== obj ) {
if (this.hasOwnProperty(newKey)) {
console.log("Inherited object updates own copy of '", prop, "' in ", newKey)
return this[newKey] = val;
} else {
console.log("Inherited object creates own copy of '", prop, "' in ", newKey)
}
} else {
console.log("Updating my own '", prop, "' in ", newKey );
}
console.log(" ->", val, " on ", this )
debugger;
return this[newKey] = val;
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment