ES6链接访问下一个方法的内容

时间:2019-11-30 22:35:50

标签: javascript ecmascript-6

我对ES6类链接有疑问。我有一个如示例中的类。我想访问prefix()的值。我想在group(),get()和post()中使用prefix()的值。我该怎么办?

class Test {

  constructor() {
    this.prefixValue = null
  }

  get(path, controller) {
    console.log(path, controller)
    return this
  }

  post(path, controller) {
    console.log(path, controller)
    return this
  }

  group(cb) {
    cb()
    console.log(this.prefixValue)
    return this
  }

  prefix(prefix) {
    this.prefixValue = prefix
    console.log(this.prefixValue)
    return this
  }

}

module.exports = new Test()

import Test from './router.js'

Test.get('/', 'IndexController.find')

Test.group(() => {
  Test.get('/', 'MovieController.find')
  Test.post('/', 'MovieController.create')
}).prefix('/movies')

0 个答案:

没有答案