Is it valid to use && instead of if?

前端 未结 3 1645
萌比男神i
萌比男神i 2021-01-20 01:01

I am using && like this and it works

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

instead

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 01:09

    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.

提交回复
热议问题