之前对此的讨论:Inheritance TypeScript with exported class and modules
这是从 ValidatorMessage.class.ts 生成的源:
///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var message = require("./Message.class");
var ValidatorMessage = (function (_super) {
__extends(ValidatorMessage, _super);
function ValidatorMessage() {
_super.apply(this, arguments);
}
return ValidatorMessage;
})(message.Message);
exports.ValidatorMessage = ValidatorMessage;
但是当我在浏览器上加载它时(在 Grunt.js 合并和缩小之后),我得到这个错误:
未捕获类型错误:无法读取未定义的属性“原型(prototype)”
就在行之后:
__.prototype = b.prototype, d.prototype = new __();
(~第 6 和第 7 行)
我不明白这个错误。即使我只放置从 TS 生成的源代码而没有 Grunt,我也会得到同样的错误。
编辑:
控制台日志(消息):
function Message(message, data, status) {
"undefined" == typeof data && (data = !1), "undefined" == typeof status && (status = !1),
/**
* Constants.
*/
this.FIELD_NAME_MESSAGE = "m", this.FIELD_NAME_DATA = "d", this.FIELD_NAME_STATUS = "s",
this.EXCEPTION_BAD_JSON_CONTENT = 'Unable to parse JSON. Bad object/string attributes. (Missing message ("' + this.FIELD_NAME_MESSAGE + '" field) or status ("' + this.FIELD_NAME_MESSAGE + '" field)?',
this.EXCEPTION_BAD_JSON_TYPE = "Incorrect data type. Object or string expected.",
this._message = message, this._data = data, this._status = status;
}
最佳答案
由于您正在编译浏览器端,因此您需要使用 --module amd
选项进行编译。您似乎已使用 --module commonjs
进行编译,这对浏览器目标无效。
关于javascript - TypeScript 的继承在浏览器端不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20264906/