javascript - TypeScript 的继承在浏览器端不起作用

标签 javascript node.js inheritance typescript extends

之前对此的讨论: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 进行编译,这对浏览器目标无效。

参见:http://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

关于javascript - TypeScript 的继承在浏览器端不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20264906/

相关文章:

JavaScript 设置公共(public)变量

node.js - 如何将 Nuxt.js 部署到 Cpanel v80 -​​ 带/不带入口点

c++ - 从派生类访问基类公共(public)成员

c# - 从非抽象类继承

javascript - 如何修复加载时在网页上显示翻译键的 Angular 翻译?

javascript - 从动态文本区域获取值

node.js - MongoDB 按 ID 分组,然后按日期分组

c++ - 为什么无法识别我的 NAN_METHOD?

javascript - JavaScript 中的原型(prototype)继承约定

javascript - 通过单击子区域中的按钮关闭模态内联对话框并防止其重新打开