Last active
August 29, 2015 14:19
-
-
Save leohxj/6106f45ecaa1e3993b0c to your computer and use it in GitHub Desktop.
Helper function for extending objects
This file contains hidden or 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
| /** | |
| * Check if object is part of the DOM | |
| * @constructor | |
| * @param {Object} obj element to check | |
| */ | |
| function isDOMElement(obj) { | |
| return obj && typeof window !== 'undefined' && (obj === window || obj.nodeType); | |
| } | |
| /** | |
| * Helper function for extending objects | |
| */ | |
| function extend (object /*, objectN ... */) { | |
| if(arguments.length <= 0) { | |
| throw new Error('Missing arguments in extend function'); | |
| } | |
| var result = object || {}, | |
| key, | |
| i; | |
| for (i = 1; i < arguments.length; i++) { | |
| var replacement = arguments[i] || {}; | |
| for (key in replacement) { | |
| // Recurse into object except if the object is a DOM element | |
| if(typeof result[key] === 'object' && ! isDOMElement(result[key])) { | |
| result[key] = extend(result[key], replacement[key]); | |
| } | |
| else { | |
| result[key] = result[key] || replacement[key]; | |
| } | |
| } | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment