Skip to content

Instantly share code, notes, and snippets.

@joshblack
Created April 10, 2015 16:14
Show Gist options
  • Save joshblack/b9341ab330fdf216dc92 to your computer and use it in GitHub Desktop.
Save joshblack/b9341ab330fdf216dc92 to your computer and use it in GitHub Desktop.
ES7 Decorators
class Foo {
@log
bar(a, b) {
console.log(`Adding ${a} and ${b} gives us ${a + b}`);
}
}
function log(target, name, descriptor) {
let fn = descriptor.value;
descriptor.value = function (...args) {
console.log('Calling', name, 'with', args);
fn.apply(this, args);
}
}
const f = new Foo();
f.bar(1, 2);
// Calling bar with [1, 2]
// Adding 1 and 2 gives us 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment