js-状态模式-用于频繁操作
连贯动作时使用
class SuperMarry {
constructor () {
this._currentState = []
this.states = {
jump () {
console.log('跳跃')
},
move () {
console.log('移动')
},
shoot () {
console.log('射击')
},
squat () {
console.log('蹲下')
}
}
}
change (arr) {
this._currentState = arr
return this
}
go () {
console.log('触发动作')
this._currentState.forEach(T => this.states[T] && this.states[T]())
return this
}
}
new SuperMarry().change(['jump', 'move']).go().change(['jump']).go()
new SuperMarry().change(['jump', 'move', 'squat']).go()

浙公网安备 33010602011771号