【发布时间】:2013-11-28 11:25:54
【问题描述】:
之前的讨论: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 合并和缩小之后),我得到了这个错误:
Uncaught TypeError: Cannot read property 'prototype' of undefined
就在该行之后:
__.prototype = b.prototype, d.prototype = new __();
(~线 6 & 7)
我不明白这个错误。即使我只放置从 TS 生成的源代码而不使用 Grunt,我也会得到同样的错误。
编辑:
console.log(消息):
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;
}
【问题讨论】:
-
行后的消息是什么:
var message = require("./Message.class");你可以在 Chrome 或 firefox 中使用 console.log(message) 来记录它并查看它的价值。它应该是一个函数,所以你也可以console.log(typeof message);看看它是否是一个函数。 -
主帖已编辑。这是一个功能。我自己编写了函数 require ,因为它以前不存在: require = function(file){ var files = file.split('/');文件 = 文件[files.length - 1].replace('.class', '');返回出口[文件] ||窗口[文件]; }
-
最后我使用了 requireJs 和 AMD 编译标志,它正在工作。
标签: javascript node.js inheritance typescript extends