未捕获到的SyntaxError:意外令牌。如果是其他陈述

时间:2018-07-17 17:48:31

标签: javascript global-variables

它不起作用,并说出Uncaught SyntaxError:意外令牌。:

var UNITS = {
    if (localStorage.getItem('lang')=='zh'){//if language chinese then it is translated Newton
   fullText:{  
        N: "牛顿",
   }, 
   }
   else (
   text:{  
        N: "N",
    } 
    )

它先于

var UNITS = {
   fullText:{  //how each unit will be printed in the dropdown box
        N: "Newton",
   }, 
text:{  //how each unit will be printed in text
        N: "N",
}
}

1 个答案:

答案 0 :(得分:1)

可以使用三元数来确定要使用的值

var fullText = localStorage.getItem('lang') == 'zh' ? '牛顿' : 'N';

var UNITS = {
  fullText: { 
    N: fullText,
  },
  text: {
    N: "N",
  }
}