Last active
January 18, 2019 06:43
-
-
Save rxluz/dab2d0292d2c10bb3e893efbff633674 to your computer and use it in GitHub Desktop.
JS Design Patterns: Decorator, see more at: https://medium.com/p/ea34a6c10423
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
| class User { | |
| constructor({ name, continent, email, allowCookies = true }) { | |
| this.setName(name); | |
| this.setEmail(email); | |
| this.setContinent(continent); | |
| if (continent === "Europe") { | |
| this.setAllowCookies(allowCookies); | |
| } | |
| } | |
| setAllowCookies(allowCookies) { | |
| this.allowCookies = allowCookies; | |
| } | |
| setName(name) { | |
| this.name = name; | |
| } | |
| setContinent(continent) { | |
| this.continent = continent; | |
| } | |
| setEmail(email) { | |
| this.email = email; | |
| } | |
| deleteAllMyData() { | |
| if (continent === "Europe") { | |
| delete this.name; | |
| delete this.continent; | |
| delete this.email; | |
| } | |
| } | |
| } | |
| const run = () => { | |
| const userRicardoInstance = new User({ | |
| name: "Ricardo", | |
| continent: "Europe", | |
| email: "[email protected]", | |
| allowCookies: false, | |
| }); | |
| console.log(userRicardoInstance); | |
| const userRobInstance = new User({ | |
| name: "Rob", | |
| continent: "America", | |
| email: "[email protected]", | |
| }); | |
| console.log(userRobInstance); | |
| }; | |
| run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment