0

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

4

1 回答 1

3

do 函数实际上是存在的,如果你执行

new Test().do()

你会得到“测试”

如果您想要智能感知类型支持,请先尝试添加构造函数。它将创建函数变量并绑定它们,如下所示:

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

这将允许您显式调用它们。

于 2012-08-20T19:05:00.390 回答