Skip to content

Instantly share code, notes, and snippets.

@basvanmeurs
Last active February 5, 2019 15:52
Show Gist options
  • Save basvanmeurs/cbcff0604a9b1268630e6cdd324e48ba to your computer and use it in GitHub Desktop.
Save basvanmeurs/cbcff0604a9b1268630e6cdd324e48ba to your computer and use it in GitHub Desktop.
State Subclassing example for inheritance
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