javascript - TypeScript class function not available -
i'm trying call instance method of typescript class (in asp.net mvc project). however, @ runtime exceptions 0x800a01b6 - javascript runtime error: object doesn't support property or method 'checkstring'
.
i copied generated javascript in jsfiddle method seems work.
i'm not javascript guy, appreciated!
things have tried far:
- different browsers (chrome:
uncaught typeerror: undefined not function
, ff:typeerror: this.checkstring not function
) - clearing browser caches
- deleting temporary files of iis express
- cleaning , rebuilding solution
- not using private modifier
- starting project on machine
- replacing underscore.js call dummy verfiy that's not problem
- checked instance members correctly set
this typescript code:
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 validate function (from within same module) var form = getformdata(); //returns formdata instance if (!form.validate()) { //foo }
and here generated 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 because of wrong this
@ runtime. can use lambda function ()=>{}
instead of function
make sure this
lexically scoped in 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)); }
please search this
means in javascript , typescript learn more.
Comments
Post a Comment