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

后端 未结 10 971
忘掉有多难
忘掉有多难 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:11

    I think that it is terrible to use constructions like this. It works but code not readable. If you want to write one-line conditions you can start using CoffeeScript and write:

    a = 0 if (typeof a == 'undefined');
    

    In your case, when you have one variable in condition and assignment, use one-line javascript ternary operator:

    a = (typeof a == 'undefined') ? 0 : a;
    

提交回复
热议问题