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

后端 未结 19 1477
攒了一身酷
攒了一身酷 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:33

    Yes.

    var data = {
        'PropertyA': 1,
        'PropertyB': 2,
        'PropertyC': 3
    };
    
    data["PropertyD"] = 4;
    
    // dialog box with 4 in it
    alert(data.PropertyD);
    alert(data["PropertyD"]);

提交回复
热议问题