Created
February 20, 2015 13:29
-
-
Save maciejsmolinski/7694c175d5e647f77fb6 to your computer and use it in GitHub Desktop.
ES6 AOP Wallet
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
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