Skip to content

Instantly share code, notes, and snippets.

@franciscotln
Last active February 25, 2017 16:26
Show Gist options
  • Save franciscotln/c13985b87d7ccc6fd1aa658266f346ec to your computer and use it in GitHub Desktop.
Save franciscotln/c13985b87d7ccc6fd1aa658266f346ec to your computer and use it in GitHub Desktop.
mixins and composition
function compose(...fns) {
return arg => fns.reduceRight((prev, curr) => curr(prev), arg);
}
const fatherMixin = (BaseClass = class {}) =>
class Father extends BaseClass {
constructor() {
super()
this.frontendLang = 'JavaScript';
}
};
const motherMixin = (BaseClass = class {}) =>
class Mother extends BaseClass {
constructor() {
super()
this.backendLang = 'Elixir';
}
};
const secretGun = Symbol(); // To be used as a private class method
const Parents = compose(fatherMixin, motherMixin)();
class Child extends Parents {
constructor(mothersSurname, fathersSurame) {
super()
this.name = `John ${mothersSurname} ${fathersSurame}`;
}
[secretGun]() { // class method using Symbol.
const gun = [
'\n..._...|..____________________, ,)',
'\n....../ `---___________----_____|]',
'\n...../_==o;;;;;;;;_______.:/',
'\n.....), ---.(_(__) / ',
'\n....// (..) ), ----" ',
'\n...//___// ',
'\n..//___//',
'\n.//___//'
];
console.warn(...gun);
}
};
const child = new Child('Mom', 'Dad');
console.log(child);
// It logs Child { backendLang: 'Elixir', frontendLang: 'JavaScript', name: 'John Mom Dad' }
child[secretGun]();
/* The secretGun() method cannot be called using dot notation.
* Unless you export/import the secretGun Symbol you won't be
* able to call this method.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment