admin管理员组文章数量:1336006
I'm writing a small example to validate Name
, Email
, Password
and ConfirmPassword
via typescript.
I've tried:
interface IValidation {
CheckingNameAndEmail(): boolean;
CheckingPasswordAndConfirmPassword(): boolean;
}
class Validation implements IValidation {
private Input: string;
private Type: string;
constructor(input: string, _type: string) {
this.Input = input;
this.Type = _type;
}
Validate = function () {
switch (this.Type) {
case 'Name':
case 'Email':
return this.CheckingNameAndEmail()
case 'Password':
case 'ConfirmPassword':
return this.CheckingPasswordAndConfirmPassword()
}
};
CheckingNameAndEmail = function () {
var reg = this.Type == 'Name'
? new RegExp('^.{4,16}$')
: new RegExp('^((([0-9]?)[a-zA-Z0-9]([0-9]?))+[\._-]??[a-zA-Z0-9]+)+@{1}?([a-zA-Z0-9]+[\._-]??[a-zA-Z0-9]+)+\.(|net|org|vn){1}$')
return reg.test(this.Input)
};
CheckingPasswordAndConfirmPassword = function () {
var reg = new RegExp('^.{6,50}$');
return reg.test(this.Input)
};
}
As you can see, I must
use this
keyword in the example. If not, it will throw me error (Like: Cannot find ...
).
So my question is: How to avoid calling this
multiple times in typescript?
I'm writing a small example to validate Name
, Email
, Password
and ConfirmPassword
via typescript.
I've tried:
interface IValidation {
CheckingNameAndEmail(): boolean;
CheckingPasswordAndConfirmPassword(): boolean;
}
class Validation implements IValidation {
private Input: string;
private Type: string;
constructor(input: string, _type: string) {
this.Input = input;
this.Type = _type;
}
Validate = function () {
switch (this.Type) {
case 'Name':
case 'Email':
return this.CheckingNameAndEmail()
case 'Password':
case 'ConfirmPassword':
return this.CheckingPasswordAndConfirmPassword()
}
};
CheckingNameAndEmail = function () {
var reg = this.Type == 'Name'
? new RegExp('^.{4,16}$')
: new RegExp('^((([0-9]?)[a-zA-Z0-9]([0-9]?))+[\._-]??[a-zA-Z0-9]+)+@{1}?([a-zA-Z0-9]+[\._-]??[a-zA-Z0-9]+)+\.(|net|org|vn){1}$')
return reg.test(this.Input)
};
CheckingPasswordAndConfirmPassword = function () {
var reg = new RegExp('^.{6,50}$');
return reg.test(this.Input)
};
}
As you can see, I must
use this
keyword in the example. If not, it will throw me error (Like: Cannot find ...
).
So my question is: How to avoid calling this
multiple times in typescript?
-
2
Why do you want to avoid
this
? Typing? – hansmaad Commented Jan 20, 2016 at 8:32 - @hansmaad Yes. Shorthand for typing. – Tân Commented Jan 20, 2016 at 8:36
2 Answers
Reset to default 6You have to explicitly write this
in Typesript. Sometimes it might be usefull to use an additional local variable to save typing and to write minification friendly code. Consider this example:
foo() {
if (this.someAwesomeMember.bar) {
var f = this.someAwesomeMember.bar(123);
var b = this.someAwesomeMember.bar(321);
// do more stuff with this awesome member
}
}
Since we're referencing the awesome member using this
all the time, there is not much to minify here. We can do better:
foo() {
var someAwesomeMember = this.someAwesomeMember;
if (someAwesomeMember.bar) {
var f = someAwesomeMember.bar(123);
var b = someAwesomeMember.bar(321);
// do more stuff with this awesome member
}
}
Which can be minified to something like
var a=this.someAwesomeMember;if(a.bar){var f=a.bar(123);var b=a.bar(321);}
Note, that it might decrease readability to replace this
by another local variable. So you have to judge carefully here.
You can't avoid it. It's a property of the language.
<hard-learned-lesson>
I tend to avoid these kind of shortcuts when I write my code because it makes it harder for others to read it. Readability is a key thing when you target to write good code (for yourself and for others).</hard-learned-lesson>
本文标签: javascriptDo we have to use quotthisquot keyword in typescriptStack Overflow
版权声明:本文标题:javascript - Do we have to use "this" keyword in typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741799910a2398158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论