Last active
December 30, 2015 10:39
-
-
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
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
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