I have a base object that I define using an object literal:
var obj = {
key1 : 'value1',
key2 : 'value2'
}
Now, I want to pass this object to a function and extend it like this:
myFunction( obj + { key3 : 'value3' } );
// The parameter will be:
{
key1 : 'value1',
key2 : 'value2',
key3 : 'value3'
}
or
myFunction( obj + { key2 : 'new value2' } );
// The parameter will be:
{
key1 : 'value1',
key2 : 'new value2'
}
The use of the +
operator in this context is not correct. Is there a way to achieve this?
EDIT: Do you want to permanently alter obj
? - No, I would like to be able to reuse it as a base for the next call.