下面的内容可以稍微DRYed一下吗?
if(totals[label]) {
totals[label] += increment;
} else {
totals[label] = increment;
}基本上,我有一个特殊的情况,当totals[label] === undefined时,因为undefined + increment === NaN typeof increment === 'number'。
发布于 2013-02-14 06:59:47
我觉得这完全没问题,你没有重复太多。是的,您可以使用
totals[label] = (totals[label] || 0) + increment;但是,这并不能节省太多。我认为
if (label in totals)
totals[label] += increment;
else
totals[label] = increment;更容易阅读,因为它更好地表达了你想要做的事情。
发布于 2013-02-14 06:57:58
totals[label] = (totals[label] || 0) + increment;https://stackoverflow.com/questions/14864876
复制相似问题