【发布时间】:2015-01-30 17:54:54
【问题描述】:
我可能在做一些明显错误的事情,但我似乎无法从咖啡脚本类的构造函数中调用实例方法。
这也可能是我犯的一个更普遍的错误,所以我将向您展示它发生的背景。
我有两个类:BaseView 和 ChildView。 ChildView 扩展 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
子视图:
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: {} }
您可以看到某些东西以某种方式与范围混淆,但我不知道是什么。我怀疑这可能与循环依赖有关......请在这里帮助我!提前致谢
【问题讨论】:
标签: javascript node.js coffeescript