Is it valid to use && instead of if?

纵饮孤独 提交于 2020-11-29 07:32:29

问题


I am using && like this and it works

typeof foo === 'function' && foo(); //if foo exist then call it

instead of

if (typeof foo === 'function') { foo(); }

Is it wrong to do or just a matter of style and taste? For me it is natural and I want to use &&, but now a linter complained: Expected an assignment or function call and instead saw an expression.

Can there be any real issues here or is it just a matter of convention?


Here is a code snippet:

function foo(x) {
  console.log("foo say " + x)
}

function bar(x) {
  console.log("bar say " + x)
}

let s = "OK"

typeof foo === 'function' && foo(s)

if (typeof bar === 'function') bar(s)

/*
   Function noo() does not exist.
   Error to try call it is prevented by the check.
   noo && noo() is not enough, so typeof is a must! 
*/
typeof noo === 'function' && noo()

console.log("OK so far")

Notes

  • To clarify my purpose was to use && as a check for existence (declared and defined).
  • If the left hand side of && fails the right hand side will not execute
  • It is useful in return and assignments too, but if is not. If an else-part is wanted, then use ?. The then else parts has to return same type.
  • I missed typeof at first and have corrected but see in comments it miss. Maybe common mistake or just easy writing while we all show understanding. But to be correct (i think) - the only way to check existence is with typeof, instanceof or try except window things you can do for example history && history.back().
  • try { foo(); }; catch (e) {}; can be used. At least one catch clause, or a finally clause, must be present.
  • if (a()) {b(); c()} equals a() && (b(), c()) because functions can be both in statements and expressions. Use comma operator.
  • The extreme is that the function is not declared and the other extreme is when function has already returned a value x = x || foo() it need not to return again (that is called memoization of a deterministic function)

回答1:


The linter's job is to look out for things that while syntactically valid may not follow recommended best practices.

"Expected an assignment or function call" probably means that it expects foo to be foo() or foo = in the first part, that seeing it without a call is, in its opinion, a mistake.

You're free to do whatever you want. Short-circuit evaluation behaves predictably, so it's going to work, but it may be confusing to people unfamiliar with that style.




回答2:


In the background there is a bit more with the && thingy, but for 99% of the cases it is perfectly fine to do it that way, just like this one.

Now as a personal opinion, for a one-liner I prefer it the && way instead of the if one, because I can't stand if keyword without a block below it hehe.

If you know what you are doing, linters are too picky sometimes.




回答3:


You could use a void operator. This evaluates the expression and returns undefined.

void (foo && foo());

var foo;

void (foo && foo());

foo = () => console.log('foo');

void (foo && foo());


来源:https://stackoverflow.com/questions/56653302/is-it-valid-to-use-instead-of-if

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