Shortcut for logical operation

依然范特西╮ 提交于 2019-12-31 04:21:45

问题


I was just wondering, is there a shortcut for logical operator(&&, ||). Like if I want to do something like i = i + 10, I can do i += 10

Reason I'm searching this is because I have a validation function which is divided into several functions. Following is a simulation:

function f1(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f2(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f3(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f4(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function validate(){
  var valid = true;
  valid = valid && f1();
  valid = valid && f2();
  valid = valid && f3();
  valid = valid && f4();
  
  console.log(valid);
}

validate();

I have tried &=

function f1(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f2(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f3(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f4(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function validate(){
  var valid = true;
  valid &= f1();
  valid &= f2();
  valid &= f3();
  valid &= f4();
  
  console.log(valid);
}

validate();

Now this can works, since true & false = 0 and 0 is false, but this looks more like a hack and was wondering if there is a better way to do such task?

Note:

I have tried valid = f1() && f2() && f3 && f4();, but in this approach, if any function returns false, subsequent functions are not executed.

Edit 1 - Nina's Suggestion

function f1(){
  console.log("f1");
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f2(){
  console.log("f2");
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f3(){
  console.log("f3");
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f4(){
  console.log("f4");
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function validate(){
  var valid = true;
  var validateFuncList = [f1,f2,f3,f4];
  valid = validateFuncList.every(function (f) { return f(); });
  console.log(valid);
}

validate();

Now this is a great answer, but this stops if any one returns false, which is same as valid = f1() && f2() && f3 && f4();

Edit 1

Just a minor update. Instead of doing: valid = valid && func1() do valid = func1() && valid. First approach will not call func1 if valid is false.


回答1:


Maybe you use an array. It calls all functions.

function validate() {
    return [f1(), f2(), f3()].every(Boolean);
}

or with an array as parameter

function validate(a) {
    return a.every(Boolean);
}

// call
xy = validate([f1(), f2(), f3()]);



回答2:


Validate them all at once to make it shorter.

function validate(){
    var result = f1() && f2() && f3() && f4();
    console.log(result);
    return result;
}

Basically you only check the next f function if the previous is true, that's the same that you do in your code, except it doesn't continue if it doesn't need to (if one of the previous is false).

See: JavaScript Comparison and Logical Operators




回答3:


Here's another approach which is much more concise. Bitwise-and doesn't short-circuit, so all functions will execute.

function validate(){
  var valid = !!(f1() & f2() & f3() & f4());
  console.log(valid);
}

Of course & is bitwise-and, not logical-and, but as long as your functions return booleans this makes no difference. At the end you can cast to boolean using !! if this is important.



来源:https://stackoverflow.com/questions/35149909/shortcut-for-logical-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!