I have object literal what I consider as base object:
var obj = {
key1 : 'value1',
key2 : 'value2'
}
and would like to use this object and pass it to function with extention like
myFunction( obj + { key3 : 'value3' } );
// param became:
{
key1 : 'value1',
key2 : 'value2',
key3 : 'value3'
}
or
myFunction( obj + { key2 : 'new value2' } );
// param became:
{
key1 : 'value1',
key2 : 'new value2'
}
+ operator is not correct for it. How I can do it? Is it a way ?
EDIT: Do you want to permanently alter obj? – No, I would like to able to reuse it for next call as base.
If you’re okay with altering
obj, just change it before you pass it:Okay, so you need to make a copy of
obj, and alter the copy — either before you callmyFunction:or pass
objand the “extension” tomyFunction:and have
myFunctiondo the work:If you’re already (or don’t mind) using jQuery,
$.extend()will be your best friend for the task.