无法在es6继承的类中设置属性

时间:2016-02-17 14:22:21

标签: javascript ecmascript-6 babeljs

我的基类在这里:Game Object

class GameObject { // Represents any interactable object within the game, be it the snake or the food or the snake world itself
  constructor(x = 0, y = 0, width = 0, height = 0) {
    this._x = x;
    this._y = y;
    this._width = width;
    this._height = height;
  }

  get x() {
    return this._x;
  }

  set x(x) {
    this._x = x;
  }

  get y() {
    return this._y;
  }

  set y(y) {
    this._y = y;
  }

  get position() {
    return {
      x: this._x,
      y: this._y
    }
  }

  set position(pos) {
    this._x = pos.x;
    this._y = pos.y;
  }

  get width() {
    return this._width;
  }

  set width(width) {
    this._width = width;
  }

  get height() {
    return this._height;
  }

  set height(height) {
    this._height = height;
  }

  get dimensions() {
    return {
      width: this._width,
      height: this._height
    }
  }

  set dimensions(dimensions) {
    this._width = dimensions.width;
    this._height = dimensions.height;
  }
}

我的孩子课在这里:Child class

class Snake extends GameObject {
  constructor() {
    super.dimensions = {width: 3};
  }
}

问题是当Babel运行此代码时,它不会在最终的JS输出中生成构造函数方法。我只是想在蛇类构造函数中设置蛇的宽度。我希望宽度由父类定义。

1 个答案:

答案 0 :(得分:0)

试试这个:

72
72
72
73
73
73
74
74
74