Is it possible to add dynamically named properties to JavaScript object?

后端 未结 19 1959
攒了一身酷
攒了一身酷 2020-11-21 05:39

In JavaScript, I\'ve created an object like so:

var data = {
    \'PropertyA\': 1,
    \'PropertyB\': 2,
    \'PropertyC\': 3
};

Is it poss

19条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 06:19

    It can be useful if mixed new property add in runtime:

    data = { ...data, newPropery: value}
    

    However, spread operator use shallow copy but here we assign data to itself so should lose nothing

提交回复
热议问题