[英]TypeScript class function not available
I'm trying to call a instance method of a TypeScript class (in an ASP.NET MVC project). 我正在尝试调用TypeScript类的实例方法(在ASP.NET MVC项目中)。 However, at Runtime I get exceptions like
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'checkString'
. 但是,在运行时我得到像
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'checkString'
这样的0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'checkString'
。
I copied the generated JavaScript in a jsfiddle where the method seems to work. 我在jsfiddle中复制了生成的JavaScript,其中方法似乎有效。
I'm not really a JavaScript guy, so any help is much appreciated! 我不是一个真正的JavaScript人,所以非常感谢任何帮助!
Things I have tried so far: 到目前为止我尝试过的事情:
Uncaught TypeError: undefined is not a function
, FF: TypeError: this.checkString is not a function
) Uncaught TypeError: undefined is not a function
,FF: TypeError: this.checkString is not a function
) This is the TypeScript code: 这是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
}
And here the generated JavaScript: 这里生成的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;
})();
This is probably because of the wrong this
at runtime. 这可能是因为错误的
this
在运行时。 You can use a lambda function ()=>{}
instead of function
to make sure that the this
is lexically scoped in the generated JavaScript: 您可以使用lambda函数
()=>{}
而不是function
,以确保在this
在生成的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));
}
Please search for what this
means in javascript and typescript to learn more. 请在javascript和打字稿中搜索
this
意味着什么来了解更多信息。
Another Bypass-Style Solution: 另一种旁路式解决方案:
instead of using this.
而不是使用
this.
, you can use super.
,你可以使用
super.
. 。
super.myMethod();
super.myMethod();
类使用super.myMethod();
构造函数中调用Method super.myMethod();
instead of this.myMethod();
this.myMethod();
This is a subtle benefit made easily possible thanks to Typescript. 由于Typescript,这是一个微妙的好处。 :)
:)
Example: 例:
Source: Typescript Bypass Solution on Stackoverflow 来源: Stackoverflow上的Typescript Bypass解决方案
export class myBaseClass
{
constructor(ctx:any)
{
this.ctx = ctx; // Audio context saved into member variable of class
}
myBaseMethod()
{
// Do Complex Work
}
}
export class myUsableClass extends myBaseClass
{
constructor(ctx:any)
{
super(ctx);
super.myBaseMethod(); // Use super., Not this.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.