【发布时间】:2015-05-22 16:06:48
【问题描述】:
我想轻松地对其进行格式化,以便任何人都可以理解最终输出。否则我想使用 if 条件重新编码。任何人都可以帮助我理解以下代码并以更易于理解的方式对其进行格式化。
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
}
【问题讨论】:
-
在
this.getType()上使用开关似乎是一个更好的解决方案。 (感谢@Barmar 纠正大脑放屁)。 -
无法将其格式化以使其易于理解。嵌套三元组本质上是令人困惑的,只是不要这样做。
-
是的,那段代码是一团乱七八糟的东西。您可以寻找在线 javascript 格式化程序。
-
我偶尔会在嵌套很短的时候进行一层嵌套,我永远不会做你正在尝试的事情。
-
那些
!1实例完全是超现实的。就好像这段代码被设计得尽可能令人讨厌。
标签: javascript