Last active
January 16, 2020 00:31
-
-
Save doriansmiley/83438c252842694bf51260d9df676e8a to your computer and use it in GitHub Desktop.
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
| export const mixin = <T>(target, sub, params = null): T => { | |
| // IMPORTANT: mixin is designed to function like Object.assign, just respects accessors | |
| // grab enumerable properties of the target object | |
| const keys = Object.keys(sub); | |
| keys.forEach((prop) => { | |
| const methodDef = Object.getOwnPropertyDescriptor(sub, prop); | |
| // check if this property uses accessor methods (Object.assign can't do this!) | |
| if (methodDef && (methodDef.get || methodDef.set)) { | |
| // yep | |
| addProperty(target, prop, Object.getOwnPropertyDescriptor(sub, prop).get, | |
| Object.getOwnPropertyDescriptor(sub, prop).set); | |
| } else { | |
| // nope | |
| target[prop] = sub[prop]; | |
| } | |
| }); | |
| return target; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment