Skip to content

Instantly share code, notes, and snippets.

@maciejsmolinski
Created February 20, 2015 13:29
Show Gist options
  • Save maciejsmolinski/7694c175d5e647f77fb6 to your computer and use it in GitHub Desktop.
Save maciejsmolinski/7694c175d5e647f77fb6 to your computer and use it in GitHub Desktop.
ES6 AOP Wallet
class AOP {
static extend(target) {
Object.assign(target, AOP.prototype);
}
}
AOP.prototype.before = function (property, fn) {
let oldProperty = this[property];
this[property] = function () {
fn.apply(this, arguments);
return oldProperty.apply(this, arguments);
}
}
AOP.prototype.after = function (property, fn) {
let oldProperty = this[property];
this[property] = function () {
oldProperty.apply(this, arguments);
return fn.apply(this, arguments);
}
}
class Wallet {
constructor (initialAmount) {
this.amount = initialAmount || 0;
}
add (amount) {
this.amount += Number(amount) || 0;
}
remove (amount) {
this.amount -= Number(amount) || 0;
}
}
class WalletWithStorage extends Wallet {
constructor (...args) {
AOP.extend(this)
super(...args);
this.after('add', this._store);
this.after('remove', this._store);
}
_store () {
localStorage.setItem('amount', this.amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment