Javascript TypeScript类函数不可用

Javascript TypeScript类函数不可用,javascript,typescript,Javascript,Typescript,我试图调用TypeScript类的实例方法(在ASP.NETMVC项目中)。但是,在运行时我会遇到异常,如0x800a01b6-JavaScript运行时错误:对象不支持属性或方法“checkString” 我将生成的JavaScript复制到一个方法似乎有效的地方。 我不是一个真正的JavaScript爱好者,所以非常感谢您的帮助 到目前为止我已经尝试过的事情: class FormData { BlogName: string; CacheTimeOut: number;

我试图调用TypeScript类的实例方法(在ASP.NETMVC项目中)。但是,在运行时我会遇到异常,如
0x800a01b6-JavaScript运行时错误:对象不支持属性或方法“checkString”

我将生成的JavaScript复制到一个方法似乎有效的地方。
我不是一个真正的JavaScript爱好者,所以非常感谢您的帮助

到目前为止我已经尝试过的事情:

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
}
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;
})();
  • 不同的浏览器(Chrome:
    uncaughttypeerror:undefined不是函数
    ,FF:
    TypeError:this.checkString不是函数
  • 清除浏览器缓存
  • 删除IIS Express的临时文件
  • 清洗和重建溶液
  • 不使用私有修饰符
  • 在另一台计算机上启动项目
  • 将underline.js调用替换为verfiy的伪调用,这不是问题所在
  • 已检查实例成员的设置是否正确
  • 这是类型脚本代码:

    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
    }
    
    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;
    })();
    
    这里是生成的JavaScript:

    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
    }
    
    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
    。您可以使用lambda函数
    ()=>{}
    而不是
    函数
    ,以确保
    在生成的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中的含义以了解更多信息。

    另一个旁路样式解决方案:
    您可以使用
    super.
    ,而不是使用
    this.

    • 先决条件是创建两个类,一个作为基类,另一个作为可用类
    • 基类包含要在构造函数中调用的方法
    • 可用类使用
      super.myMethod()从其构造函数中调用该方法
      而不是
      this.myMethod()
    由于Typescript,这是一个很容易实现的细微好处。:)

    示例:
    资料来源:


    谢谢,在将函数作为Jquery事件调用时,我遇到了同样的问题。“this”指的是事件,而不是课堂。你的解决方案解决了我的问题。它仍然不能解释为什么在Typescript中会发生这种情况。一定还有别的办法。六年后我才遇到这个问题。令人震惊和失望的是,它现在还没有修好