Can I write an if statement within a Javascript object when setting an attribute?

前端 未结 5 1017
自闭症患者
自闭症患者 2021-01-03 23:30

Setting attributeTwo using an if statement. What is the correct way to do this?

var testBoolean = true;

var object = {
  attributeOne: \"attributeOne\",
  a         


        
5条回答
  •  囚心锁ツ
    2021-01-04 00:11

    No, however you can use the ternary operator:

    var testBoolean = true;
    
    var object = {
      attributeOne: "attributeOne",
      attributeTwo: testBoolean ? "attributeTwo" : "attributeTwoToo"
    }
    

提交回复
热议问题