如何将此三元转换为if / else?

时间:2016-03-04 20:42:36

标签: javascript

vertex1 --created--> vertex3
vertex1 --knows--> vertex2
vertex1 --knows--> vertex4

如何将此转换为if else语句?

2 个答案:

答案 0 :(得分:3)

你不应该做额外的检查

var mWeight; 
if(model && pToolCat === 3 && pToolItems.length){ // check this one only
  mWeight = pToolWeight()
} else {
 mWeight = null; // else null anyway
}

答案 1 :(得分:0)

以你重复的方式

const mWeight;
if(model && !pToolCat === 3 )
mWeight=null;
else if(model && pToolCat === 3 && !pToolItems.length)
mWeight=null;
else if(model && pToolCat === 3  && pToolItems.length)
mWeight=pToolWeight();
else
mWeight=null;

但那是浪费时间。始终使用简单的编码

const mWeight;
if(model && pToolCat === 3 && pToolItems.length)
mWeight = pToolWeight();
else
mWeight = null;