Last active
January 4, 2016 17:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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