我在扩展项目时遇到问题,我得到的是:
Uncaught TypeError: Cannot read property 'prototype' of undefined
根据我的阅读,项目需要按特定顺序定义,所以这就是我正在做的,因为看起来它们的顺序是正确的。
这不会发生在编译时,而是在浏览器运行时发生。我正在使用 browserify 将这些文件编译成一个文件和 tsify .
这是我的入口点main.ts:
import GameSmartWeb from './GameSmartWeb';
window.gs = new GameSmartWeb();
然后它调用此文件 GameSmartWeb.ts,它引用了一个 GUI 类:
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
});
最佳答案
这件事今天发生在我身上,虽然它似乎与你的原因不同。无论如何,我正在为将来来到这里的其他人写一个答案。
我的文件是这样设置的:
item.ts:
export class Item {
myAwesomeFunction() {
...
}
}
index.ts(为了能够顺利引用):
...
export * from './item';
...
other.item.ts:
import { ..., Item, ... } from './index';
export class OtherItem extends Item ... {
...
}
这导致了我的错误:
Cannot read property 'prototype' of undefined
将 other.item.ts 更改为以下内容后,它起作用了:
import { ... } from './index';
import { Item } from './item';
export class OtherItem extends Item ... {
...
}
关于javascript - 扩展类时无法读取未定义的属性 'prototype',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708304/