Last active
February 5, 2019 15:52
-
-
Save basvanmeurs/cbcff0604a9b1268630e6cdd324e48ba to your computer and use it in GitHub Desktop.
State Subclassing example for inheritance
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
import Account from "./Account.js"; | |
export default class StealingAccount extends Account { | |
constructor() { | |
super(); | |
this._stolen = 0; | |
} | |
getRealAvailable() { | |
return this._balance; | |
} | |
getAvailable() { | |
// Rounding to 1 decimal will obscure our stolen cents. | |
return Math.round(super.getAvailable() / 10) * 10; | |
} | |
getStolenAmount() { | |
return this._stolen; | |
} | |
static _states() { | |
const states = super._states(); | |
const openIndex = states.findIndex(state => state.name === "Open"); | |
states[openIndex] = class Open extends states[openIndex] { | |
deposit(amount) { | |
super.deposit(amount - 0.01); | |
this._stolen += 0.01; | |
} | |
}; | |
return states; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment