Last active
August 23, 2017 20:54
-
-
Save koshuang/f11f1fbfb2d1bcb0c72dd2f9e7f5a5fe 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
| const composeMixins = (...fns) => ( | |
| obj = {}, | |
| piped = x => fns.reduce((o, fn) => fn(o), x) | |
| ) => piped(obj); | |
| const user = { | |
| name: 'kos', | |
| email: 'test@test.com', | |
| }; | |
| const nameTrait = obj => ({ ...obj, getName: () => obj.name }); | |
| const emailTrait = obj => ({ ...obj, getEmail: () => obj.email }); | |
| const mixins = composeMixins(nameTrait, emailTrait); | |
| const newUser = mixins(user); | |
| console.log(newUser.getName()); // kos | |
| console.log(newUser.getEmail()); // test@test.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a slightly better version:
Note that you can customize the mixing behavior. For example, if you want to compose with first-in precedence instead of last-in: