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 に答える