使用 coffeescript 获取超出范围的 class 的变量

Get variable of a class out of the scope with coffeescript

我有那个代码:

class @Validator


  ##
  # Constructor
  #
  # Set basic variables
  #
  ##
  constructor: ->

    @_errors = {}

  ##
  # Errors
  ##
  errors:

    first: ->

    last: ->

    all: =>

      return @_errors 

    get: ->

在对象errors的方法all()中无法到达变量_errors的内容,怎么可能到达?

如果你要的API是这个

validator = new Validator()
validator.errors.all()

然后将错误对象放入构造函数中并更改

errors:
    first: ->

errors =
    first: ->

否则,只需更改

errors:
    first: ->

errors: ->
    first: ->

使 API 看起来像这样 validator.errors().all()