Last active
February 25, 2017 16:26
-
-
Save franciscotln/c13985b87d7ccc6fd1aa658266f346ec to your computer and use it in GitHub Desktop.
mixins and composition
This file contains 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
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