我在這裏做錯了什麼?我爲我的回調使用了胖箭頭=>,但是當代碼達到cb.onEndElement並調用@returner時,我得到一個空對象異常。那麼,爲什麼@returner不存在?爲了將其綁定到的Parser
實例CoffeeScript範圍問題
class Parser
constructor:() ->
@returner = (data) ->
searchParser: new xml.SaxParser (cb) =>
cb.onStartElementNS (elem, attrs, prefix, url, ns) =>
if elem is "results" then @results = []
else if elem is "title" then @curr = "title"
else @curr = "none"
cb.onCdata (cdata) =>
if @curr is "title" then @book.title = cdata
cb.onEndElementNS (elem, prefix, url) =>
@results.push @book if elem is "book"
cb.onEndDocument =>
@returner @results
search: (str, callback) ->
@returner = callback
@searchParser.parseString str
p = new Parser
p.search "somexml", (data) ->
console.log JSON.stringify data
我的答案解決了您的問題嗎? – nicolaskruchten 2011-05-18 22:43:14
是的,謝謝。 – Matthew 2011-05-23 19:32:41