Skip to content

Instantly share code, notes, and snippets.

@jacky810124
Last active March 28, 2017 07:58
Show Gist options
  • Save jacky810124/729dfc6413fa8a5bef4465c3f69be78d to your computer and use it in GitHub Desktop.
Save jacky810124/729dfc6413fa8a5bef4465c3f69be78d to your computer and use it in GitHub Desktop.
class State {
approve() {
throw 'Should override this function'
}
reject() {
throw 'Should override this function'
}
}
class Verifying extends State {
approve(data) {
console.log('verify succeed', data)
}
reject() {
console.log('verify failed')
}
}
class VerifyFailed extends State {
reject() {
console.log('verifying')
}
}
class VerifySucceed extends State {
}
class Teacher {
constructor(state) {
const states = { Verifying, VerifySucceed, VerifyFailed }
this.currentState = new states[state]()
this.approve = this.currentState.approve
this.reject = this.currentState.reject
}
}
const teacherA = new Teacher('Verifying')
teacherA.approve({ name: 'jacky' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment