One key distinction that stands out is the number of parameters accepted by each method - append
takes two, while merge
can take two or more.
Update:
Upon inspecting the code within these methods, another contrast emerges. The merge
function will duplicate the properties from other objects whereas append
simply copies them over.
Therefore, if one of the properties in an object or array is shared across multiple objects, modifying it through append
will affect the original object as well:
var firstObj = {
name: 'John',
lastName: [ 'Doe' ]
};
var secondObj = {
age: '20',
sex: 'male',
lastName: [ 'Dorian' ]
};
//Object.merge(firstObj, secondObj);
Object.append(firstObj, secondObj);
console.log(firstObj.lastName[0]); // Dorian
secondObj.lastName[0] = 'McEnroe';
console.log(firstObj.lastName[0]); // McEnroe
Conversely, using merge
preserves the independence between the array items in firstObj.lastName
and secondObj.lastName
. They remain distinct objects.
Fiddle: http://jsfiddle.net/Guffa/PrsXj/
Furthermore, the merge
method offers a different functionality when the second parameter is a string:
merge(obj, "name", obj2)
This operation solely transfers the property obj2.name
to obj
, essentially equivalent to merge(obj, { name: obj2.name })
.