javascript中的链式三元运算符
我想格式化它很容易,所以任何人都可以理解最终输出。否则我想使用if条件重新编码。谁能帮助我理解下面的代码,并以更直观的理解方式格式化它javascript中的链式三元运算符,javascript,Javascript,我想格式化它很容易,所以任何人都可以理解最终输出。否则我想使用if条件重新编码。谁能帮助我理解下面的代码,并以更直观的理解方式格式化它 validate: function() { return this.getRequired() && !this.getValue() ? (this.showError("Required"), !1) : this.getValue() ? "email" != this
validate: function() {
return this.getRequired() &&
!this.getValue() ?
(this.showError("Required"), !1)
: this.getValue() ?
"email" != this.getType() || this.isValidEmail() ?
"email_username" != this.getType() || this.isValidEmail() || this.isValidUsername() ?
"username" != this.getType() || this.isValidUsername() ?
"roomid" != this.getType() || this.isValidUsername() ?
"displayName" != this.getType() || this.isValidDisplayName() ?
"phone" != this.getType() || this.isValidPhone() ? !0
: (this.showError("Enter a valid phone number"), !1)
:(this.showError("Enter both first and last names"), !1)
:(this.showError("Enter a valid meeting room ID"), !1)
:(this.showError("Enter a valid username"), !1)
:(this.showError("Enter a valid email address or username"), !1)
:(this.showError("Enter a valid email address"), !1)
: !0
}
不确定这是否有效
var items = ['email', 'email_username', 'etc'];
for(var i=0;i<items.length;i++){
switch(items[i]){
case 'email':
if(items[i] != this.getType || items[i].isValidEmail())
this.showError('Enter a valid email address'), !1)
break;
case 'email_username':
if(items[i] != this.getType || items[i].isValidEmail() || this.isValidUsername())
this.showError('Enter a valid email address'), !1)
break;
}
}
var items=['email','email_username','etc'];
对于(var i=0;i这是代码的一部分,我只针对电子邮件执行了此操作。其余部分您需要执行以下操作:
validate: function () {
var type = this.getType();
var isValid = true;
if (this.getRequired() && !this.getValue()) {
switch (type) {
case "email":
if (!this.isValidEmail()) {
isValid = false;
this.showError("Enter a valid email address")
}
break;
case "email_username":
break;
case "username":
break;
case "roomid":
break;
case "displayName":
break;
case "phone":
break;
}
}
return isValid;
}
这实际上不是不可读的。是格式不好。我想这样格式化:
return this.getRequired() && !this.getValue()
? (this.showError("Required"), false)
: this.getValue()
? "email" != this.getType() || this.isValidEmail()
? "email_username" != this.getType() || this.isValidEmail() || this.isValidUsername()
? "username" != this.getType() || this.isValidUsername()
? "roomid" != this.getType() || this.isValidUsername()
? "displayName" != this.getType() || this.isValidDisplayName()
? "phone" != this.getType() || this.isValidPhone()
? true
: (this.showError($L("Enter a valid phone number")), false)
: (this.showError("Enter both first and last names"), false)
: (this.showError("Enter a valid meeting room ID"), false)
: (this.showError("Enter a valid username"), false)
: (this.showError("Enter a valid email address or username"), false)
: (this.showError("Enter a valid email address"), false)
: true
它看起来确实很粗糙,但它节省了大量单独的返回语句,如“如果这个,那么返回这个,如果这个,那么返回这个…”等等
还有,是的,!1
语法是完全多余的
编辑:这里开关不起作用的原因是因为逻辑是嵌套的。你要么用嵌套的if语句替换ternaries,要么你必须重写逻辑。如果它起作用了……太好了。让它进行测试,看看是否有可能重构。如果没有……我希望你有这方面的业务规则。wh您不能使用以下更可读的格式:
validate: function () {
var ok = false;
switch (true) {
case (this.getRequired() && !this.getValue()):
this.showError("Required");
break;
case ("email" === this.getType() && !this.isValidEmail()):
this.showError("Enter a valid email address");
break;//
case ("email_username" === this.getType() && (!this.isValidEmail() || !this.isValidUsername())):
this.showError("Enter a valid email address or username");
break;
case ("username" === this.getType() && !this.isValidUsername()):
this.showError("Enter a valid username");
break;
case ("roomid" === this.getType() && !this.isValidUsername()):
this.showError("Enter a valid meeting room ID");
break;
case ("displayName" === this.getType() && !this.isValidDisplayName()):
this.showError("Enter both first and last names");
break;
case ("phone" !== this.getType() && !this.isValidPhone()):
this.showError($L("Enter a valid phone number"));
break;
default: ok = true;
}
return ok;
}
在这里使用开关this.getType()
似乎是一个更好的解决方案。(感谢@Barmar纠正大脑放屁)。没有办法对其进行格式化以使其易于理解。嵌套的三元组本质上是令人困惑的,不要这样做。是的,那段代码太乱了。你可以找一个在线javascript格式化程序。我偶尔会在很短的时间内进行一级嵌套,我永远不会做你正在尝试的事情。那些!1
实例完全是超现实的。就像这段代码被设计成尽可能令人讨厌的迟钝。虽然没有那么高效,但在进行表单验证时更容易遵循,效率从来都不是一个问题。用户每秒可以执行多少次点击?半打?