无法在 es6 继承的类中设置属性
Unable to set property in es6 inherited class
我的基类在这里:游戏对象
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;
}
}
我的孩子班在这里:儿童班
class Snake extends GameObject {
constructor() {
super.dimensions = {width: 3};
}
}
问题是当 Babel 运行这段代码时,它不会在最终的 JS 输出中生成构造函数方法。我只是想在蛇类构造函数中设置蛇的宽度。我希望宽度由父类定义。
试试这个:
class Snake extends GameObject {
constructor(...args) {
super(...args);
this.dimensions = {width: 3};
}
}
相关文章:
- Javascript使用变量设置属性
- 如何在自动完成时设置属性标题
- 未捕获的类型错误:无法设置属性'innerHTML'如果为null,则将脚本移动到正文不会;不起作用
- 未捕获的类型错误:无法设置属性'背景'的未定义
- 无法设置属性'innerHTML'在javascript中为null
- Haxe Javascript:在不使用Reflect的情况下按名称获取和设置属性
- Jquery为每个元素设置属性
- 从骨干集合筛选模型,然后为这些模型设置属性
- 我们可以设置属性'id'到不同的'按钮'通过使用javascript或jquery的循环
- 如何在javascript对象中设置属性的类型,就像mongoose模式设计一样
- emscripten+sdl=引发异常:TypeError:无法设置属性'widthNative'的未定
- 未捕获的类型错误:无法设置属性'onclick'为null.已尝试window.onload
- Backbone JS Promises在模型上设置属性之前解析
- Highcharts无法设置属性'的值;要点':对象为null或未定义
- 无法设置属性'round'的未定义
- 通过 Javascript 设置 C# 属性值
- 在 app.locals 上设置属性和调用 app.set() 有什么区别?
- 不引人注目地设置属性后函数失败
- j查询在设置属性后在后续单击时不读取数据属性
- IE错误:无法设置属性'的值;样式':对象为null或未定义