Extending an object with multiple properties in JavaScript

后端 未结 3 1678
清酒与你
清酒与你 2021-02-19 16:39

I have a global JavaScript object with multiple properties and functions that I am creating it this way:

myObject = {};

I thought that I can ea

3条回答
  •  我寻月下人不归
    2021-02-19 17:00

    Without jQuery, you could create an array of objects with something like this:

    [{'key': 'atuhoetu', 'value': 'uehtnehatn'},{...}]
    

    If you don't care about compatibility, recent browsers should support this:

    var newObj = {prop1: 'val', prop2: 'val'};
    Object.keys(newObj).forEach(function (item) {
        myObject[item] = newObj[item];
    });
    

    This will iterate over all items in newObject and add them to myObj.

提交回复
热议问题