TypeScript类函数不可用 [英] TypeScript class function not available
问题描述
我正在尝试调用TypeScript类的实例方法(在ASP.NET MVC项目中)。但是,在运行时我得到的异常如 0x800a01b6 - JavaScript运行时错误:对象不支持属性或方法'checkString'
。
I'm trying to call a instance method of a TypeScript class (in an ASP.NET MVC project). However, at Runtime I get exceptions like 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'checkString'
.
我将生成的JavaScript复制到 jsfiddle 这个方法似乎有用。
我不是一个真正的JavaScript人,所以非常感谢任何帮助!
I copied the generated JavaScript in a jsfiddle where the method seems to work.
I'm not really a JavaScript guy, so any help is much appreciated!
东西到目前为止我已尝试过:
- 不同的浏览器(Chrome:
未捕获TypeError:undefined不是function
,FF:TypeError:this.checkString不是函数
) - 清除浏览器缓存
- 删除IIS Express的临时文件
- 清理并重建解决方案
- 不使用私有修饰符
- 在另一台机器上启动项目
- 用假人替换underscore.js调用以验证这不是问题
- 检查实例成员是否已正确设置
- different browsers (Chrome:
Uncaught TypeError: undefined is not a function
, FF:TypeError: this.checkString is not a function
) - clearing browser caches
- deleting the temporary files of IIS Express
- cleaning and rebuilding the solution
- not using the private modifier
- starting the project on another machine
- replacing the underscore.js call with a dummy to verfiy that's not the problem
- checked that the instance members are correctly set
这是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;
})();
推荐答案
这可能是因为错误的这个
在运行时。您可以使用lambda函数()=> {}
而不是函数
来确保此
在生成的JavaScript中具有词法范围:
This is probably because of the wrong this
at runtime. You can use a lambda function ()=>{}
instead of function
to make sure that the this
is lexically scoped in the generated JavaScript:
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));
}
请搜索此
在javascript和typescript中表示了解更多信息。
Please search for what this
means in javascript and typescript to learn more.
这篇关于TypeScript类函数不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!