0

コードを実行すると、次のエラーが表示されます。

TypeError: ピボットが定義されていません

エラーが参照している関連クラスは次のとおりです。

class Hero
  constructor: (@color, @direction, @x, @y) ->
  pivots: []
  addPivot: (pivot) -> @pivots.push(pivot)
  onPivot: (pivot) -> pivot.x == @x && pivot.y == @y
  applyPivots: () ->
    indexToRemove = -1
    for i in [0..@pivots.length - 1]
      pivot = @pivots[i]
      if (@onPivot(pivot))
        @direction = pivot.direction
        indexToRemove = i
    @pivots.splice(indexToRemove, 1)

なぜこうなった?生成しているJavaScriptは次のとおりです。

  Hero = (function() {
    function Hero(color, direction, x, y) {
      this.color = color;
      this.direction = direction;
      this.x = x;
      this.y = y;
    }

    Hero.prototype.pivots = [];

    Hero.prototype.addPivot = function(pivot) {
      return this.pivots.push(pivot);
    };

    Hero.prototype.onPivot = function(pivot) {
      return pivot.x === this.x && pivot.y === this.y;
    };

    Hero.prototype.applyPivots = function() {
      var i, indexToRemove, pivot, _i, _ref;

      indexToRemove = -1;
      for (i = _i = 0, _ref = this.pivots.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
        pivot = this.pivots[i];
        if (this.onPivot(pivot)) {
          this.direction = pivot.direction;
          indexToRemove = i;
        }
      }
      return this.pivots.splice(indexToRemove, 1);
    };

    return Hero;

  })();

エラーは次の行にあります。

  return pivot.x === this.x && pivot.y === this.y;
4

1 に答える 1