我有一个const
numberOfComments
这是一个数组的大小,我想要另一个const
riskByComments
其值为 5 如果numberOfComments
大于2,否则应为0。
我知道我可以轻松做到这一点let
,但我知道最好只使用const
export const calculateRisk = (ticket) => dispatch => {
const numberOfComments = ticket.comments.length
let riskByComments//if i use const i need to declare its value
//right away, i cannot use an if statement to do it later
if(ticket.comments.length>2){
riskByComments=5//if i use const inside the if statement
}else{
riskByComments=0//i cannot use it in the rest of my function
}
使用条件运算符:
const riskByComments = numberOfComments >2 ? 5 : 0;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)