简体   繁体   English

TypeScript类函数不可用

[英]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: 到目前为止我尝试过的事情:

  1. different browsers (Chrome: Uncaught TypeError: undefined is not a function , FF: TypeError: this.checkString is not a function ) 不同的浏览器(Chrome: Uncaught TypeError: undefined is not a function ,FF: TypeError: this.checkString is not a function
  2. clearing browser caches 清除浏览器缓存
  3. deleting the temporary files of IIS Express 删除IIS Express的临时文件
  4. cleaning and rebuilding the solution 清理和重建解决方案
  5. not using the private modifier 不使用私有修饰符
  6. starting the project on another machine 在另一台机器上启动项目
  7. replacing the underscore.js call with a dummy to verfiy that's not the problem 用假人替换underscore.js调用来验证这不是问题
  8. checked that the instance members are correctly set 检查实例成员是否已正确设置

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. .

  • A prerequisite is to create two classes, one as a Base Class, another as a Usable Class. 先决条件是创建两个类,一个作为基类,另一个作为可用类。
  • The Base Class contains the methods that you want to call in the constructor. Base Class包含要在构造函数中调用的方法。
  • The Usable Class calls the Method from within it's constructor using 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM