Skip to content

Instantly share code, notes, and snippets.

@dalgard
Last active December 30, 2015 10:39
Show Gist options
  • Save dalgard/7817327 to your computer and use it in GitHub Desktop.
Save dalgard/7817327 to your computer and use it in GitHub Desktop.
Method for shallow extension of a given object with the properties of passed-in object(s) with support for standards-compliant getters and setters
function extend(target) {
// Run through rest parameters
Array.prototype.slice.call(arguments, 1).forEach(function (source) {
if (source) {
// If source is an array, only copy enumerable properties
var keys = (Array.isArray(source) ? Object.keys(source) : Object.getOwnPropertyNames(source));
// Iterate over keys
keys.forEach(function (key) {
// Standards-compliant awesomeness
var descriptor = Object.getOwnPropertyDescriptor(source, key);
Object.defineProperty(target, key, descriptor);
});
}
});
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment