Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function interceptObjectProperties(obj, options){
if (!options) options = {};
if (!options.ignore) options.ignore = {};
for (var i in obj) {
const prop = i;
if (!obj.hasOwnProperty(prop)) continue;
if (options.ignore[prop]) continue;
var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
if (descriptor.set) {
const original = descriptor.set;
descriptor.set = function(value){
console.log('set', prop, value)
return original.call(this, value)
}
options.log_interceptable && console.log('intercepted setter '+prop);
}
if (descriptor.get) {
const original = descriptor.get;
descriptor.get = function(){
var ret = original.apply(this, arguments)
console.log('get', prop, ret)
return ret;
}
options.log_interceptable && console.log('intercepted getter '+prop)
}
if (descriptor.value) {
if (typeof descriptor.value === 'function') {
const original = descriptor.value;
descriptor.value = function(){
console.log('method ', prop, arguments)
return original.apply(this, arguments);
}
options.log_interceptable && console.log('intercepted function '+prop)
}
}
Object.defineProperty(obj, prop, descriptor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.