扩展类时无法读取未定义的属性“原型”

新手上路,请多包涵

我在扩展项目时遇到问题,我得到:

未捕获的类型错误:无法读取未定义的属性“原型”

从我读过的内容来看,项目需要以特定的顺序定义,所以这就是我正在做的,因为看起来它们的顺序是正确的。

这不会发生在编译时,而是在浏览器运行时发生。我正在使用 browserifytsify 将这些文件编译成一个文件。

这是我的入口点 main.ts

 import GameSmartWeb from './GameSmartWeb';
window.gs = new GameSmartWeb();

然后它调用这个引用 GUI 类的文件 GameSmartWeb.ts

 import GUI from './apis/GUI';
export default class GameSmartWeb {
    public get gui(): GUI { return new GUI(); }
}

然后 GUI 类 apis/GUI.ts 看起来有点像这样:

 export default class GUI extends GameSmartWeb {
    public get rewards(): Rewards { return new Rewards(); }
}

class Rewards extends GUI {
    // More methods
}

在浏览器中查看时,它说错误在这里:

 var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); // The error is on this line
};
var GUI = (function (_super) {
    __extends(GUI, _super); // This is the call to the function
    // more auto generated code
});

原文由 Get Off My Lawn 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 231
1 个回答

这件事今天发生在我身上,尽管它似乎与你的原因不同。无论如何,我正在为将来来到这里的其他人写一个答案。

我的文件设置如下:

项目.ts

 export class Item {
    myAwesomeFunction() {
        ...
    }
}

index.ts (为了能够顺利引用):

 ...
export * from './item';
...

其他.item.ts

 import { ..., Item, ... } from './index';

export class OtherItem extends Item ... {
    ...
}

这导致了我的错误:

无法读取未定义的属性“原型”

other.item.ts 更改为以下内容后,它起作用了:

 import { ... } from './index';
import { Item } from './item';

export class OtherItem extends Item ... {
    ...
}

原文由 Arg0n 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题