在Javascript中,可以将三元运算符的`?放在下一行吗? [英] In Javascript, is it OK to put the ternary operator's `?` on then next line?

查看:99
本文介绍了在Javascript中,可以将三元运算符的`?放在下一行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢对齐?以及当我的三元运算符不适合一行时的::

I really like aligning the ? and the : of my ternary operator when they don't fit on a line, like this:

var myVar = (condition
    ? ifTrue
    : ifFalse
);

但是, JSHint 抱怨:

换行符在'?'之前破损

Bad line breaking before '?'

JSHint为什么会有此警告?是否有任何讨厌的东西(例如分号插入等)可以保护我免受伤害,或者我可以安全地更改我的JSHINT配置以忽略它?

Why would JSHint have this warning? Is there any nastyness (like semicolon insertion, etc) it is protecting me against or can I safely change my JSHINT configuration to ignore it?

推荐答案

这有效并且肯定有效.在嵌套案例等更复杂的用例中特别有用.

This works and is certainly valid. It's especially useful in more complicated use cases, like nested ones.

var a = test1
         ? b
         : test2
            ? c
            : d;

这篇关于在Javascript中,可以将三元运算符的`?放在下一行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆