我正在尝试调用 TypeScript 类的实例方法(在 ASP.NET MVC 项目中)。但是,在运行时我遇到了类似的异常0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'checkString'
.
我将生成的 JavaScript 复制到jsfiddle http://jsfiddle.net/H7FL9/2/该方法似乎有效的地方。
我不是一个真正的 JavaScript 爱好者,所以非常感谢您的帮助!
到目前为止我尝试过的事情:
- 不同的浏览器(Chrome:
Uncaught TypeError: undefined is not a function
, FF: TypeError: this.checkString is not a function
)
- 清除浏览器缓存
- 删除IIS Express的临时文件
- 清理并重建解决方案
- 不使用 private 修饰符
- 在另一台机器上启动项目
- 用虚拟替换 underscore.js 调用以验证这不是问题
- 检查实例成员是否正确设置
这是 TypeScript 代码:
class FormData {
BlogName: string;
CacheTimeOut: number;
CopyrightHolder: string;
NavBarTitle: string;
MarkdownExtra: boolean;
MarkdownSanitize: boolean;
RatingActive: boolean;
HtmlEditor: boolean;
constructor(blogName: string, cacheTimeOut: number, copyrightHolder: string, navBarTitle: string, markdownExtra: boolean, markdownSanitize: boolean, ratingActive: boolean, htmlEditor: boolean) {
this.BlogName = blogName;
this.CacheTimeOut = cacheTimeOut;
this.CopyrightHolder = copyrightHolder;
this.NavBarTitle = navBarTitle;
this.MarkdownExtra = markdownExtra;
this.MarkdownSanitize = markdownSanitize;
this.RatingActive = ratingActive;
this.HtmlEditor = htmlEditor;
}
private checkString(value: string): boolean {
return _.isString(value) && value !== '';
}
validate(): boolean {
return (this.checkString(this.BlogName) && this.checkString(this.CopyrightHolder) && this.checkString(this.NavBarTitle) && _.isNumber(this.CacheTimeOut) && !_.isNull(this.MarkdownExtra) && !_.isNull(this.MarkdownSanitize) && !_.isNull(this.RatingActive));
}
}
//I'm calling the validate function like that (from within the same module)
var form = getFormData(); //returns a FormData instance
if (!form.validate()) {
//foo
}
这里是生成的 JavaScript:
var FormData = (function () {
function FormData(blogName, cacheTimeOut, copyrightHolder, navBarTitle, markdownExtra, markdownSanitize, ratingActive, htmlEditor) {
this.BlogName = blogName;
this.CacheTimeOut = cacheTimeOut;
this.CopyrightHolder = copyrightHolder;
this.NavBarTitle = navBarTitle;
this.MarkdownExtra = markdownExtra;
this.MarkdownSanitize = markdownSanitize;
this.RatingActive = ratingActive;
this.HtmlEditor = htmlEditor;
}
FormData.prototype.checkString = function (value) {
return _.isString(value) && value !== '';
};
FormData.prototype.validate = function () {
return (this.checkString(this.BlogName) && this.checkString(this.CopyrightHolder) && this.checkString(this.NavBarTitle) && _.isNumber(this.CacheTimeOut) && !_.isNull(this.MarkdownExtra) && !_.isNull(this.MarkdownSanitize) && !_.isNull(this.RatingActive));
};
return FormData;
})();