Created
January 18, 2019 06:44
-
-
Save rxluz/4682090d20c65867a4236e08da43ca0b 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
class User { | |
constructor({ name, continent, email }) { | |
this.setName(name); | |
this.setEmail(email); | |
this.setContinent(continent); | |
} | |
setName(name) { | |
this.name = name; | |
} | |
setContinent(continent) { | |
this.continent = continent; | |
} | |
setEmail(email) { | |
this.email = email; | |
} | |
} | |
class UserGPDRDecorator { | |
constructor({ UserInstance, allowCookies = true }) { | |
if (UserInstance.continent === "Europe") { | |
UserInstance.setAllowCookies = this.setAllowCookies; | |
UserInstance.deleteAllMyData = this.deleteAllMyData; | |
UserInstance.setAllowCookies(allowCookies); | |
} | |
return UserInstance; | |
} | |
setAllowCookies(allowCookies) { | |
this.allowCookies = allowCookies; | |
} | |
deleteAllMyData() { | |
if (this.continent === "Europe") { | |
delete this.name; | |
delete this.continent; | |
delete this.email; | |
delete this.allowCookies; | |
} | |
} | |
} | |
const run = () => { | |
const userRicardoInstance = new UserGPDRDecorator({ | |
UserInstance: new User({ | |
name: "Ricardo", | |
continent: "Europe", | |
email: "[email protected]", | |
}), | |
allowCookies: false, | |
}); | |
console.log(userRicardoInstance); | |
userRicardoInstance.deleteAllMyData(); | |
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