【发布时间】:2012-08-21 04:08:35
【问题描述】:
出于某种原因,我用 CoffeeScript 编写的类没有复制原型。
这是我正在使用的确切代码:
module.exports = class ListQueue
constructor: ->
@queue = []
@queueIds =[]
@currentId = 0
# Adds the given element to the queue.
# Returns the index of the element.
add: (el) ->
@queueIds.push @currentId
@queue.push el
@currentId++ # return @lastIndex, then increment
shift: ->
@queueIndexes.shift()
@queue.shift()
# Returns the index in the @queue array for the given id.
queueIndex: (id) ->
for queueIndex, i in queueIndexes
if queueIndex is id
return i
undefined
get: (id) ->
index = @queueIndex id
@queue[index]
length: ->
@queue.length
remove: (id) ->
index = @queueIndex id
if index?
@queueIds = @queueIds.slice index, index
@queue = @queue.slice index, index
true
else
false
这是类的原型,正如预期的那样:
{ add: [Function],
shift: [Function],
queueIndex: [Function],
get: [Function],
length: [Function],
remove: [Function] }
但这很奇怪:new ListQueue 实际上给了我这个:
{ queue: [], queueIds: [], currentId: 0 }
我正在使用 Node.JS 0.8.7。
【问题讨论】:
-
如果您发布一些 JavaScript,也许有人可以帮助您。
-
我不知道coffeescript,但是你能提供编译好的JavaScript吗?
-
您的代码应该可以工作。你能发布一些代码来证明
new Test()上没有.do吗? -
抱歉这个简短的问题,我用一些实际代码扩展了它。
标签: javascript node.js coffeescript