在扩展类时无法读取未定义的属性"prototype"

Get*_*awn 8 javascript typescript

我有扩展项目的问题,我得到的地方:

未捕获的TypeError:无法读取undefined的属性'prototype'

从我所看到的内容中,需要按特定的顺序定义项目,所以这就是我正在做的事情,因为看起来它们的顺序正确.

这不会在编译时发生,而是在运行时在浏览器中发生.我编译文件合并成一个文件,browserifytsify.

这是我的切入点main.ts:

import GameSmartWeb from './GameSmartWeb';
window.gs = new GameSmartWeb();
Run Code Online (Sandbox Code Playgroud)

然后它调用此文件GameSmartWeb.ts引用GUI类:

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

那么GUI类apis/GUI.ts看起来有点像这样:

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

class Rewards extends GUI {
    // More methods
}
Run Code Online (Sandbox Code Playgroud)

在浏览器中查看它时出现错误:

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
});
Run Code Online (Sandbox Code Playgroud)

Arg*_*g0n 7

今天发生在我身上,虽然它似乎与你没有相同的原因.无论如何,我正在为将来来这里的其他人写一个答案.

我的文件设置如下:

item.ts:

export class Item {
    myAwesomeFunction() {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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

...
export * from './item';
...
Run Code Online (Sandbox Code Playgroud)

other.item.ts:

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

export class OtherItem extends Item ... {
    ...
}
Run Code Online (Sandbox Code Playgroud)

这导致了我的错误:

无法读取undefined的属性'prototype'

other.item.ts更改为以下内容后,它可以工作:

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

export class OtherItem extends Item ... {
    ...
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我遇到过同样的问题。就我而言,捆绑配置文件中类文件的顺序是其原因。我在基类之一之前指定了派生类的文件,切换顺序,修复了它。

  • 哇,这实际上是解决方案 - 令人难以置信的令人沮丧。 (3认同)