首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >继承TypeScript (AMD)扩展类包含在扩展类中。

继承TypeScript (AMD)扩展类包含在扩展类中。
EN

Stack Overflow用户
提问于 2013-11-29 23:15:04
回答 2查看 518关注 0票数 0

我想了解为什么这个代码:

代码语言:javascript
复制
///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>

import message = require("Message.class");

export class ValidatorMessage extends message.Message{
    private _errors: Array;

    constructor(message: string){
        super(message);
    }
}

生成这个.js文件:

代码语言:javascript
复制
///<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 __();
};

define(["require", "exports", "Message.class"], function(require, exports, __message__) {
    var message = __message__;

    var ValidatorMessage = (function (_super) {
        __extends(ValidatorMessage, _super);
        function ValidatorMessage(message) {
            _super.call(this, message);
        }
        return ValidatorMessage;
    })(message.Message);
    exports.ValidatorMessage = ValidatorMessage;
});

为什么ValidatorMessage在消息类中?!我还是不明白。

使用amd标志编译。

编辑:

然后,我使用requireJs来要求该文件,但它之前已经与其他文件合并了。有了requireJs,我没有ValidatorMessage类,只有消息。

这是合并的文件:

代码语言:javascript
复制
/*! MotorEngine-web - v0.0.1 - 2013-11-29 09:11:18 - development */
///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
define([ "require", "exports" ], function(require, exports) {
    var Message = function() {
        function Message(message, data, status) {
            "undefined" == typeof data && (data = !1), "undefined" == typeof status && (status = !1), 

            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;
        }

        return Message.prototype.getMessage = function() {
            return this._message;
        }, 

        Message.prototype.getData = function() {
            return this._data;
        }, 
        Message.prototype.getStatus = function() {
            return this._status;
        }, 
        Message.prototype.toJSON = function() {
            return JSON.stringify(this._toSimpleObject());
        }, 
        Message.prototype.toObject = function() {
            return this._toSimpleObject();
        }, 
        Message.prototype._toSimpleObject = function() {
            var json = {};
            return json[this.FIELD_NAME_MESSAGE] = this._message, this._data !== !1 && (json[this.FIELD_NAME_DATA] = this._data), 
            json[this.FIELD_NAME_STATUS] = this._status, json;
        }, 
        Message.prototype.fromJSON = function(json) {
            if ("object" == typeof json) return this._fromJSONObject(json);
            if ("string" == typeof json) return this._fromJSONString(json);
            throw "Message.fromJSON " + this.EXCEPTION_BAD_JSON_TYPE;
        }, 
        Message.prototype._fromJSONObject = function(json) {
            if (json[this.FIELD_NAME_MESSAGE] && json[this.FIELD_NAME_STATUS]) return json[this.FIELD_NAME_DATA] ? new Message(json[this.FIELD_NAME_MESSAGE], json[this.FIELD_NAME_DATA], json[this.FIELD_NAME_STATUS]) : new Message(json[this.FIELD_NAME_MESSAGE], !1, json[this.FIELD_NAME_STATUS]);
            throw "Message._fromJSONObject " + this.EXCEPTION_BAD_JSON_CONTENT;
        }, 
        Message.prototype._fromJSONString = function(json) {
            try {
                return this._fromJSONObject(JSON.parse(json));
            } catch (e) {
                throw "Message._fromJSONString: JSON.parse error:" + e.message;
            }
        }, Message;
    }();
    exports.Message = Message;
});

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
var __extends = this.__extends || function(d, b) {
    function __() {
        this.constructor = d;
    }
    for (var p in b) b.hasOwnProperty(p) && (d[p] = b[p]);
    __.prototype = b.prototype, d.prototype = new __();
};

define([ "require", "exports", "Message.class" ], function(require, exports, __message__) {
    var message = __message__, ValidatorMessage = function(_super) {
        function ValidatorMessage(message) {
            _super.call(this, message);
        }
        return __extends(ValidatorMessage, _super), ValidatorMessage;
    }(message.Message);
    exports.ValidatorMessage = ValidatorMessage;
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-30 01:45:50

好吧,我明白了!

因为我的文件名是:

代码语言:javascript
复制
Message.class.ts
ValidatorMessage.class.ts

但是对于自动生成的TS,保留.class并使用它作为类名(而不仅仅是文件),并且它稍后会用requireJs生成一个错误。(未找到类别)

=>不再使用.class了!

谢谢!

票数 0
EN

Stack Overflow用户

发布于 2013-11-30 00:58:23

在您的代码中:

代码语言:javascript
复制
var ValidatorMessage = (function (_super) {
    __extends(ValidatorMessage, _super);
    function ValidatorMessage(message) {
        _super.call(this, message);
    }
    return ValidatorMessage;
})(message.Message);

你的问题

为什么ValidatorMessage在消息类中?

ValidatorMessage不在消息类中。消息类被传递到ValidatorMessage类的函数闭包中。

至于其原因:这使得codegen +更容易实现,良好的实践也是标准JS。假设您希望将继承从message.Message更改为Foo。您只需将})(message.Message);更改为})(Foo);,而不是每次引用_super

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20294591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档