Skip to content

Instantly share code, notes, and snippets.

@pentaphobe
Created August 1, 2017 06:38
Show Gist options
  • Save pentaphobe/8caa19f4809596ffcc8ac2c2206970a1 to your computer and use it in GitHub Desktop.
Save pentaphobe/8caa19f4809596ffcc8ac2c2206970a1 to your computer and use it in GitHub Desktop.
Auto wrapping of functions
let basis = {
hello: (name) => console.log(`hello ${name}!`)
};
function wrap(fn, name, ctx) {
return function() {
let args = Array.prototype.slice.call(arguments);
console.log(`wrapped function ${name}`, args);
return fn.apply(ctx, args);
};
}
let proxy = new Proxy(basis, {
get: function (target, name) {
if (!(name in target)) {
return wrap(() => {}, `${name}(missing)`, this);
}
return wrap(target[name], name, this);
}
});
(function() {
proxy.hello('noodle');
// wrapped function hello ["noodles"]
// hello noodles!
proxy.asdf(23, 24, 25);
// wrapped function asdf(missing) (3) [23, 24, 25]
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment