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

前端 未结 5 1043
自闭症患者
自闭症患者 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

    you can also do by this method

    var testBoolean = true;
    
    var object = {
      attributeOne: "attributeOne"
    }
    

    1

    if(testBoolean){
       object.attributeTwo = "attributeTwo"
    }else{
       object.attributeTwo = "attributeTwoToo"
    }
    

    2

    object.attributeTwo = testBoolean ? "attributeTwo" : "attributeTwoToo"
    

提交回复
热议问题