Skip to content

Instantly share code, notes, and snippets.

@rvighne
Last active January 4, 2016 17:19
Show Gist options
  • Save rvighne/8653622 to your computer and use it in GitHub Desktop.
Save rvighne/8653622 to your computer and use it in GitHub Desktop.
The modern way to deep-merge objects. It cleanly attaches itself to all Objects without showing up in `for...in` loops and modifies the object being copied into.
/*
Copyright (c) 2014 Rohit Vighne
License: The MIT License (MIT)
*/
Object.defineProperty(Object.prototype, "extend", {
value: function() {
for (var i = arguments.length; i--;) {
var obj = arguments[i];
for (var j in obj) {
var exists = j in this;
if (obj[j] instanceof Object && exists) {
this[j].extend(obj[j]);
} else if (!exists) {
this[j] = obj[j];
}
}
}
return this;
},
// Match the native functions as closely as possible
configurable: true, enumerable: false, writable: true,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment