使用&&是否有效?而不是如果? [英] Is it valid to use && instead of if?

查看:137
本文介绍了使用&&是否有效?而不是如果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用&&这样,并且可以正常工作

I am using && like this and it works

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

代替

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?

这是一个代码段:

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")

注释

  • 澄清我的目的是使用&&作为存在(已声明和定义)的检查.
  • 如果&&的左侧失败,则右侧将不执行
  • 它在return和分配中也很有用,但if却没有.如果需要 else 部分,请使用?. then else 部分必须返回相同的类型.
  • 我一开始错过了typeof并已更正,但在评论中看到它错过了.在我们大家都表现出理解的时候,也许是常见的错误,或者只是简单的写作.但是正确的是(我认为)-检查存在性的唯一方法是使用 typeof try ,但您可以执行的window除外,例如history && history.back().
  • 可以使用
  • try { foo(); }; catch (e) {};. 至少有一个catch子句,或finally子句,必须存在.
  • if (a()) {b(); c()}等于a() && (b(), c()),因为函数可以同时存在于语句和表达式中.使用逗号运算符.
  • 极端是未声明函数,而另一极端是当函数已返回值x = x || foo()时,无需再次返回(称为确定性函数的记忆)
  • 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)

推荐答案

短毛猫的工作是寻找在语法上有效的可能不遵循建议的最佳做法的事情.

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

预期的分配或函数调用"可能意味着它期望第一部分中的foofoo()foo =,在其看来,不进行调用就是一个错误.

"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.

这篇关于使用&&是否有效?而不是如果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆