Javascript AND operator within assignment

后端 未结 6 842
滥情空心
滥情空心 2020-11-22 05:20

I know that in JavaScript you can do:

var oneOrTheOther = someOtherVar || \"these are not the droids you are looking for...\";

where the va

6条回答
  •  一整个雨季
    2020-11-22 05:57

    I have been seeing && overused here at work for assignment statements. The concern is twofold: 1) The 'indicator' check is sometimes a function with overhead that developers don't account for. 2) It is easy for devs to just see it as a safety check and not consider they are assigning false to their var. I like them to have a type-safe attitude, so I have them change this:

    var currentIndex = App.instance && App.instance.rightSideView.getFocusItemIndex();

    to this:

    var currentIndex = App.instance && App.instance.rightSideView.getFocusItemIndex() || 0;

    so they get an integer as expected.

提交回复
热议问题