Skip to content

Instantly share code, notes, and snippets.

@doriansmiley
Last active January 16, 2020 00:31
Show Gist options
  • Select an option

  • Save doriansmiley/83438c252842694bf51260d9df676e8a to your computer and use it in GitHub Desktop.

Select an option

Save doriansmiley/83438c252842694bf51260d9df676e8a to your computer and use it in GitHub Desktop.
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