Coffeescript类范围问题

时间:2014-12-02 13:06:02

标签: javascript node.js coffeescript

我可能做了一些明显错误的事情,但我似乎无法从coffeescript类的构造函数中调用实例方法。

这也可能是一个更普遍的错误,所以我会告诉你它发生在哪里的背景。

我有两个类:BaseView和ChildView。 ChildView扩展了BaseView。 BaseView有一个递归方法,用一个子实例树填充其中一个属性。像这样:

BaseView:

class BaseView
  constructor: (@xml) ->
    console.log(@) #this logs X
    @.children = @.populateChildren()

  populateChildren: () ->
    out = []
    _.each(@.xml, (node) ->
      out.push(new require("./child")(node))
    )
    return out

ChildView:

BaseView = require('./base')
class ChildView extends BaseView
    constructor: (@xml) -> super

我通过以下方式初始化一个新的ChildView:

ChildView = require('./child')
childView = new ChildView(someXML)

我得到的错误是:

TypeError: Object #<Object> has no method 'populateChildren'

你可以从BaseView中的X console.log看到原因

    { ArrayBuffer: [Function: ArrayBuffer],
      Int8Array: { [Function: Int8Array] BYTES_PER_ELEMENT: 1 },
      Uint8Array: { [Function: Uint8Array] BYTES_PER_ELEMENT: 1 },
      Uint8ClampedArray: { [Function: Uint8ClampedArray] BYTES_PER_ELEMENT: 1 },
      Int16Array: { [Function: Int16Array] BYTES_PER_ELEMENT: 2 },
      Uint16Array: { [Function: Uint16Array] BYTES_PER_ELEMENT: 2 },
      Int32Array: { [Function: Int32Array] BYTES_PER_ELEMENT: 4 },
      Uint32Array: { [Function: Uint32Array] BYTES_PER_ELEMENT: 4 },
      Float32Array: { [Function: Float32Array] BYTES_PER_ELEMENT: 4 },
      Float64Array: { [Function: Float64Array] BYTES_PER_ELEMENT: 8 },
      DataView: [Function: DataView],
      global: [Circular],
      process: ...
      GLOBAL: [Circular],
      root: [Circular],
      Buffer: 
       { [Function: Buffer]
         isEncoding: [Function],
         poolSize: 8192,
         isBuffer: [Function: isBuffer],
         byteLength: [Function],
         concat: [Function],
         _charsWritten: 669 },
      setTimeout: [Function],
      setInterval: [Function],
      clearTimeout: [Function],
      clearInterval: [Function],
      setImmediate: [Function],
      clearImmediate: [Function],
      console: [Getter],
      before: [Function],
      after: [Function],
      beforeEach: [Function],
      afterEach: [Function],
      context: { [Function] skip: [Function], only: [Function] },
      describe: { [Function] skip: [Function], only: [Function] },
      xcontext: [Function],
      xdescribe: [Function],
      specify: { [Function] only: [Function], skip: [Function] },
      it: { [Function] only: [Function], skip: [Function] },
      xspecify: [Function],
      xit: [Function],
      xml: [ 'row', [ 'col', [Object], [Object] ] ],
      data: 
       { 'mentions-per-day': 
          { legend: [Object],
            tooltip: [Object],
            yAxis: [Object],
            xAxis: [Object],
            subtitle: [Object],
            title: [Object] },
         data: { 'number-of-mentions': '3', 'mentions-per-day': [Object] } },
      options: {} }

你可以看到某些东西与范围有些混乱,但我无法弄清楚是什么。我觉得它可能与循环依赖有关...请帮助我在这里任何人!提前致谢

1 个答案:

答案 0 :(得分:0)

这只是一个或多或少的明显错字:

@.children = @.populateCildren()
#                      ^

应该是

@.children = @.populateChildren()

但请注意,省略点更加惯用:

@children = @populateChildren()
相关问题