如何将三元if语句转换回标准javascript?

如何将三元if语句转换回标准javascript?,javascript,ternary-operator,Javascript,Ternary Operator,我正在处理这个三元if语句: a[textProp] = _newText + ' (' + (_newText === a[textProp] ? 'no change' : 'changed') + ')'; 我想把它转换回标准javascript(为了可读性)。此外,我还想将其转换为if、elseif、else语句,以测试变量是否为空/null 这是我所拥有的,但不起作用: if (_newText = null) { 'Invalid Record'; } else if (_newT

我正在处理这个三元if语句:

a[textProp] = _newText + ' (' + (_newText === a[textProp] ? 'no change' : 'changed') + ')';
我想把它转换回标准javascript(为了可读性)。此外,我还想将其转换为if、elseif、else语句,以测试变量是否为空/null

这是我所拥有的,但不起作用:

if (_newText = null) {
'Invalid Record';
}
else if (_newText === a[textProp]) {
'no change';
}
else(_newText != a[textProp]) {
'changed';
}
+ ')';
需要

if (_newText == null) {
             ^^

你需要建立你的关系

a[textProp] = 'Invalid Record';

为了可读性,我将从以下内容开始,其中每个状态都被显式检查(有效和更改):


但是,我也认为将测试结果存储为
a[textProp]
中的字符串是不对的,它可能会使未来的测试无效。我可能有单独的测试结果键(作为标志),例如:
a.valid[textProp]
a.changed[textProp]
(在这种情况下,
textProp
永远不能
“valid”
“changed”
)。更好的方法是将文本存储在
a[textProp].text
中,并将标志存储在
a[textProp].valid
a[textProp].changed

else
语句中使用测试不会出错?我仍然无法使无效记录正常工作。看看我的小提琴,试试看。请注意,如果您多次单击该按钮,就会发生我提到的情况。我以前确实注意到了这一点,但请查看第9个列表项。它没有记录编号,所以我希望在它后面附加类似“invalid”的内容,而不是不做任何更改。有什么想法吗?
if (_newText === null) {
             ^^^
a[textProp] = 'Invalid Record';
var isValid = true;
if (_newText == null || _newText.trim().length === 0) {
  isValid = false;
}

var hasChanged = false;
if (isValid && _newText !== a[textProp]) {
  hasChanged = true;
}

if (!isValid) {
  a[textProp] = 'Invalid Record';
}
else if (hasChanged) {
  a[textProp] = _newText + ' (changed)';
}
else {
  a[textProp] += ' (no change)';
}