[removed] || instead of IF statement - is this legal and cross browser valid?

后端 未结 10 992
忘掉有多难
忘掉有多难 2020-12-23 20:43

It seems that:

if (typeof a == \'undefined\') {
    a = 0;
}

and

(typeof a != \'undefined\') || (a = 0)

h

10条回答
  •  忘掉有多难
    2020-12-23 21:31

    IMHO || (a = 0) is way too similar to || (a == 0) and thus confusing. One day overzealous developer will just "fix it", changing the meaning of your code. And every other developer will have to sit for a while to figure out whether this was your intent or just a simple bug.

    And this is in fact what JSLint is trying to say:

    Expected a conditional expression and instead saw an assignment.

    I avoid using confusing constructs as they hurt readability. a = a || 0; is way more recognizable and similar in meaning.

提交回复
热议问题