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

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

    Stylistically, setting default values like a || a=default is a common idiom on entry into a function, because javascript doesn't enforce the number of arguments.

    Readability will be compromised if this construct is used in other circumstances, where you really mean if/else.

    Performance used to vary between the different styles, but in a quick test today if/else and logical operators were the same speed, but ternary operation was slower.

提交回复
热议问题