admin 管理员组文章数量: 1086019
I have jslint plaining me to use ||
operator for below code,
query = ['browser' + (ieVersion ? ieVersion : 'UNKNOWN')]
I tried using ||
operator but that ends up in wrong result,
query = ['browser' + ieVersion || 'UNKNOWN']
// => ['browserundefined']
I have jslint plaining me to use ||
operator for below code,
query = ['browser' + (ieVersion ? ieVersion : 'UNKNOWN')]
I tried using ||
operator but that ends up in wrong result,
query = ['browser' + ieVersion || 'UNKNOWN']
// => ['browserundefined']
Share
Improve this question
edited Dec 18, 2012 at 18:01
Mahesh Kulkarni
asked Dec 18, 2012 at 17:36
Mahesh KulkarniMahesh Kulkarni
3073 silver badges11 bronze badges
3
- Sorry my bad, I did try with parentheses. Will update the question now – Mahesh Kulkarni Commented Dec 18, 2012 at 17:55
- Um. But it works with parens, as in the answer you accepted. – Dave Newton Commented Dec 18, 2012 at 18:01
- Edit: I screwed. I actually din't use brackets. I will always worry about precedence from now on. – Mahesh Kulkarni Commented Dec 18, 2012 at 18:01
3 Answers
Reset to default 6Operator precedence is wrong, try this:
query = ['browser' + (ieVersion || 'UNKNOWN')]
without extra parentheses +
operator is stronger and JavaScript engine evaluates it as:
query = [('browser' + ieVersion) || 'UNKNOWN']
Notice that 'browser' + ieVersion
is never falsy so you'll never see 'UNKNOWN'
.
brackets?
query = ['browser' + (ieVersion || 'UNKNOWN')]
You need to wrap the expression in parentheses:
query = ['browser' + (ieVersion || 'UNKNOWN')]
本文标签: javascriptJslint use 3939 operator instead of conditional operatorStack Overflow
版权声明:本文标题:javascript - Jslint use '||' operator instead of conditional operator - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744069290a2528192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论