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()

  

posted @ 2019-10-14 15:29  fm060  阅读(189)  评论(0)    收藏  举报