Created
January 1, 2019 16:13
-
-
Save SebastianHGonzalez/9ecd08f845cb9a173b40ce5bd7b3c2a9 to your computer and use it in GitHub Desktop.
Aspects applied to instance methods
This file contains 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
function loggerAspect(joinPoint, logger) { | |
return function (...args) { | |
logger.log("logging"); | |
return joinPoint.apply(this, args) | |
} | |
} | |
class Person { | |
constructor(name) { | |
this.name = name | |
} | |
greeting() { | |
return "Hello " + this.name | |
} | |
} | |
const person = new Person("john doe"); | |
person.greeting(); | |
// <- "Hello john doe" | |
// Apply logger aspect | |
Person.prototype.greeting = loggerAspect(Person.prototype.greeting, console) | |
person.greeting(); | |
// logging | |
// <- "Hello john doe" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment