【发布时间】:2011-11-19 00:31:15
【问题描述】:
我有以下 CoffeeScript 代码示例:
class TestClass
constructor: () ->
@list = new Object()
addToList: (key, value) ->
@list[key] = value
printList: () ->
console.log("This is printed from printList:", @list)
startHttp: () ->
http = require("http")
http.createServer(@printList).listen(8080)
test = new TestClass()
test.addToList("key", "value")
test.printList()
test.startHttp()
当我运行代码并向 127.0.0.1:8080 发出 HTTP 请求时,我希望得到以下输出:
这是从 printList 打印的:{ key: 'value' }
这是从 printList 打印的:{ key: 'value' }
但我得到的是以下内容:
这是从 printList 打印的:{ key: 'value' }
这是从 printList: undefined 打印出来的
为什么printList函数从HTTP服务器调用时无法访问list变量?
我正在使用 Node.js v0.6.1 和 CoffeeScript v1.1.3。
【问题讨论】:
标签: node.js coffeescript