[英]Optimize JavaScript if else code
Is there a better way of writing the below JS code? 有没有更好的方法来编写下面的JS代码?
var check1 = model1.validateAll();
var check2 = model2.validateAll();
if (check1.isValid === false || check2.isValid === false) {
self.showValidationErrors(check1.messages);
self.showValidationErrors(check2.messages);
return true;
}
My only concern is once inside the if block, either check1.messages
OR check2.messages
can have some value (depending on check1.isValid
OR check2.isValid
is false
) 我唯一关心的是,如果在if块中,则check1.messages
或 check2.messages
可以具有某些值(取决于check1.isValid
或 check2.isValid
为false
)
First, using === false
or === true
is very rarely necessary. 首先,很少需要使用=== false
或=== true
。 Typically you just test like this: !check1.isValid
There are use cases for the === false
form, but they're rare. 通常,您只需要像这样测试: !check1.isValid
=== false
形式有一些用例,但是很少见。
The simple thing here is: 这里简单的事情是:
if (!check1.isValid && !check2.isValid) {
if (!check1.isValid) {
self.showValidationErrors(check1.messages);
}
if (!check2.isValid) {
self.showValidationErrors(check2.messages);
}
return true;
}
Or if you're in an ES5 environment (and what we have below can be shimmed): 或者,如果您处于ES5环境中(并且可以填充以下内容):
var invalid = false;
[check1, check2].forEach(function(chk) {
if (!chk.isValid) {
self.showValidationErrors(chk.messages);
invalid = true;
}
});
if (invalid) {
return true;
}
That may seem inefficient, but as I assume this is in response to a user action, the few milliseconds involved are a non-issue. 这看似效率低下,但由于我认为这是对用户操作的响应,因此所涉及的几毫秒不是问题。
Use separate if
blocks. 使用单独的if
块。 Instead of returning immediately from the block, set a variable and return it when all blocks are done. 设置变量并在完成所有块后将其返回,而不是立即从该块返回。
returnVal = false;
if (check1.isValid) {
self.showValidationErrors(check1.messages);
returnVal = true;
}
if (check2.isValid) {
self.showValidationErrors(check2.messages);
returnVal = true;
}
return returnVal;
Also, when you find yourself creating variables with names like check1
, check2
, etc, and doing the same thing with all of them, you probably should be using an array and looping over them. 另外,当您发现自己创建的变量具有诸如check1
, check2
等名称, check2
所有变量执行相同的操作时,您可能应该使用数组并对其进行循环。
For something totally different: 对于完全不同的东西:
return [model1, model2].map(function(model) {
var check = model.validateAll();
if (!check.isValid) this.showValidationErrors(check.messages);
return check.isValid;
}, self).indexOf(false) > -1;
Check for the meaning of the second argument of map
. 检查map
的第二个参数的含义。
Keep in mind that array methods like map
and indexOf
aren't supported by IE8 and lower, but they can easily be polyfilled. 请记住,IE8和更低版本不支持诸如map
和indexOf
类的数组方法,但可以轻松地对其进行填充。
I would create a function, that way you won't repeat yourself (DRY). 我会创建一个函数,这样您就不会重复自己(DRY)。
function validateModel(model, self)
{
var check = model.validateAll();
if(check === false){
self.showValidationErrors(check.messages);
}
}
Then you would just call it: 然后,您只需调用它:
validateModel(model1, self);
validateModel(model2, self);
I'd do it like this but only if it's not necessary to check both check-objects in order to write a validation message: 我会这样做,但前提是不必为了编写验证消息而检查两个检查对象:
var check1 = model1.validateAll();
var check2 = model2.validateAll();
self.showValidationErrors(check1);
self.showValidationErrors(check2);
return !check1.isValid || !check2.isValid;
And to the showValidationError(check)
-function adding this to the first row 并将其添加到showValidationError(check)
-函数到第一行
if (check.isValid) return;
I don't think you have to write both messages if just one validation isn't valid, right? 我认为如果只有一个验证无效,您不必同时写两条消息,对吗? If I'm wrong than this way is of course not the best one. 如果我做错了,那当然不是最好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.