Skip to content

Instantly share code, notes, and snippets.

@rxluz
Last active January 18, 2019 06:43
Show Gist options
  • Select an option

  • Save rxluz/dab2d0292d2c10bb3e893efbff633674 to your computer and use it in GitHub Desktop.

Select an option

Save rxluz/dab2d0292d2c10bb3e893efbff633674 to your computer and use it in GitHub Desktop.
JS Design Patterns: Decorator, see more at: https://medium.com/p/ea34a6c10423
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