簡體   English   中英

繼承TypeScript(AMD)-擴展類包含在擴展類中

[英]Inheritance TypeScript (AMD) - Extending class is included in the extended class

我想了解為什么這段代碼:

///<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文件:

///<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在Message類中? 我不明白

與amd標志一起編譯。

編輯:

然后,我使用requireJs來要求該文件,但之前它已與其他文件合並。 對於requireJs,我沒有ValidatorMessage類,只有Message。

這是合並的文件:

/*! 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;
});

在您的代碼中:

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

你的問題

為什么ValidatorMessage在Message類中?

ValidatorMessage不在Message類中。 消息類被傳遞到ValidatorMessage類的函數閉包中。

至於原因:這使得代碼生成+更加容易,而且標准JS也是一種很好的做法。 假設您要將繼承從message.Message更改為Foo。 您只需更改})(message.Message); })(Foo); 而不是每個對_super引用

好吧,我明白了!

這是因為我的文件名是:

Message.class.ts
ValidatorMessage.class.ts

但是使用自動生成的TS保留.class並將其用作Class名稱(不僅用於文件),並且稍后會在requireJs中生成錯誤。 (找不到類)

=>將不再使用.class!

謝謝 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM