-2
我可能做了一些明顯的錯誤,但我似乎無法從coffeescript類的構造函數中調用實例方法。Coffeescript類作用域問題
這也可能是一個更一般的錯誤我正在做,所以我會告訴你在哪裏發生的情況。
我有兩個類: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
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'
而且你可以看到爲什麼從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: {} }
你可以看到的東西是搞砸了與範圍莫名其妙,但我想不出什麼。我想,也許這與循環依賴有關......請幫助我在這裏任何人!在此先感謝
對於超時延遲的回覆感到抱歉 - 但即使您是對的,也不是這個原因。當我發佈這個問題時,我錯誤地輸入了它。仍然無法解決問題,但繼續前進。 如果你把所有的類放在同一個文件中,它可以工作。如果他們在不同的文件中,那麼它就成了一個問題 – 2015-03-10 15:08:21