JavaScript原型不复制

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

正如预期的那样,这是class级的原型:

  { add: [Function], shift: [Function], queueIndex: [Function], get: [Function], length: [Function], remove: [Function] } 

但是这很奇怪: new ListQueue实际上给了我这个:

  { queue: [], queueIds: [], currentId: 0 } 

我正在使用Node.JS 0.8.7。

do函数实际上在那里,如果你执行

 new Test().do() 

你会得到“testing”

如果您需要智能感知types支持,请尝试先添加构造函数。 它将创build函数variables,并将其绑定,如下所示:

 this.LoadData = __bind(this.LoadData, this); 

这将允许您明确地调用它们。