I'm working on building a JavaScript function that assigns enumerable properties of the source object(s) to the destination object. However, subsequent sources overwrite property assignments of previous sources.
Example if called as such:
extend({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' });
should return --> { 'user': 'fred', 'age': 40 }
Bonus: Use Reduce
This is an exercise/challenge for a programming boot camp and I've researched endlessly to find a solution to this. My issue here is that the test objects are not contained within an array so I cannot use reduce to manipulate them. I've attempted multiple ways of altering the test objects such Array.prototype.slice.call(arguments).
Still learning, so if there are any oversights or considerations please let me know. I've been at it for a while so I'm pretty confident there could be some simple mental mistakes
I'm currently unable to pass all the tests provided with the code I have.
Sample code:
function extend(destination) {
var args = Array.prototype.slice.call(arguments);
var result = args.reduce(function (newObj, currentObj) {
for (var key in currentObj) {
if (currentObj.hasOwnProperty(key)) {
newObj[key] = currentObj[key];
}
}
return newObj;
}, {});
return result;
}