我想了解为什么这段代码:
///<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;
});
答案 0 :(得分:1)
在您的代码中:
var ValidatorMessage = (function (_super) {
__extends(ValidatorMessage, _super);
function ValidatorMessage(message) {
_super.call(this, message);
}
return ValidatorMessage;
})(message.Message);
你的问题
为什么ValidatorMessage在Message类中?
ValidatorMessage不在Message类中。 Message类被传递给ValidatorMessage类的函数闭包。
至于原因:这使得codegen +更容易,良好的做法也是标准的JS。假设您要将继承从message.Message更改为Foo。您只需将})(message.Message);
更改为})(Foo);
,而不是每次引用_super
答案 1 :(得分:0)
好的,我明白了!
因为我的文件名是:
Message.class.ts
ValidatorMessage.class.ts
但是使用自动生成TS保留.class并将其用作类名(不仅仅用于文件),并且它稍后会在requireJs中生成错误。 (没有找到课程)
=&GT;不再使用.class了!
谢谢!