出于某种原因,我用 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。