protoMerge
by ScottRippey
HTML
(output)
JavaScript
/**
* Effectively merges the properties of all objects.
*
* Does so by altering the prototype chain,
* which is more efficient than simply copying properties.
*
*/
function protoDefaults(objects_) {
var i = arguments.length - 1, next = arguments[i];
while (i--) {
arguments[i].__proto__ = next;
next = arguments[i];
}
return next;
}
function protoMerge(objects_) {
var i = arguments.length - 1, next = arguments[i];
while (i--) {
next = next.__proto__ = arguments[i];
}
return next;
}
var merged = protoDefaults(
{ a: 'a', b: 'b' }
, { a: 'OVERRIDDEN', c: 'c' }
, { b: 'OVERRIDDEN', c: 'OVERRIDDEN', d: 'd' }
);
var output = {
a: merged.a
, b: merged.b
, c: merged.c
, d: merged.d
};
$(document.body).set('text', "Results: " + JSON.stringify(output));