Created
August 1, 2017 06:38
-
-
Save pentaphobe/8caa19f4809596ffcc8ac2c2206970a1 to your computer and use it in GitHub Desktop.
Auto wrapping of functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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