Created
April 10, 2015 16:14
-
-
Save joshblack/b9341ab330fdf216dc92 to your computer and use it in GitHub Desktop.
ES7 Decorators
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
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