我真的很喜欢对齐?以及我的三元运算符的 : 当它们不适合一行时,如下所示:
var myVar = (condition
? ifTrue
: ifFalse
);
然而,JSHint抱怨:
'?' 之前的坏换行符
为什么 JSHint 会有这个警告?是否有任何讨厌的东西(如分号插入等)可以保护我,或者我可以安全地更改我的 JSHINT 配置以忽略它吗?
我真的很喜欢对齐?以及我的三元运算符的 : 当它们不适合一行时,如下所示:
var myVar = (condition
? ifTrue
: ifFalse
);
然而,JSHint抱怨:
'?' 之前的坏换行符
为什么 JSHint 会有这个警告?是否有任何讨厌的东西(如分号插入等)可以保护我,或者我可以安全地更改我的 JSHINT 配置以忽略它吗?
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;
三元运算符在视觉上可能会令人困惑,所以 ? 问号总是开始一行并将缩进增加4个空格,并且:冒号总是开始一行,与?对齐 问号。条件应该用括号括起来。
var integer = function (
value,
default_value
) {
value = resolve(value);
return (typeof value === "number")
? Math.floor(value)
: (typeof value === "string")
? value.charCodeAt(0)
: default_value;
};
You should put the operator on the end of the line. That way its more clear that the statment continued to the next line.