Guest User

Untitled

a guest
Jan 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. class User {
  2. constructor({ name, continent, email }) {
  3. this.setName(name);
  4. this.setEmail(email);
  5. this.setContinent(continent);
  6. }
  7.  
  8. setName(name) {
  9. this.name = name;
  10. }
  11.  
  12. setContinent(continent) {
  13. this.continent = continent;
  14. }
  15.  
  16. setEmail(email) {
  17. this.email = email;
  18. }
  19. }
  20.  
  21. class UserGPDRDecorator {
  22. constructor({ UserInstance, allowCookies = true }) {
  23. if (UserInstance.continent === "Europe") {
  24. UserInstance.setAllowCookies = this.setAllowCookies;
  25. UserInstance.deleteAllMyData = this.deleteAllMyData;
  26. UserInstance.setAllowCookies(allowCookies);
  27. }
  28.  
  29. return UserInstance;
  30. }
  31.  
  32. setAllowCookies(allowCookies) {
  33. this.allowCookies = allowCookies;
  34. }
  35.  
  36. deleteAllMyData() {
  37. if (this.continent === "Europe") {
  38. delete this.name;
  39. delete this.continent;
  40. delete this.email;
  41. delete this.allowCookies;
  42. }
  43. }
  44. }
  45.  
  46. const run = () => {
  47. const userRicardoInstance = new UserGPDRDecorator({
  48. UserInstance: new User({
  49. name: "Ricardo",
  50. continent: "Europe",
  51. email: "ricardo@ricardo.com",
  52. }),
  53. allowCookies: false,
  54. });
  55.  
  56. console.log(userRicardoInstance);
  57.  
  58. userRicardoInstance.deleteAllMyData();
  59. console.log(userRicardoInstance);
  60.  
  61. const userRobInstance = new User({
  62. name: "Rob",
  63. continent: "America",
  64. email: "rob@rob.com",
  65. });
  66.  
  67. console.log(userRobInstance);
  68. };
  69.  
  70. run();
Add Comment
Please, Sign In to add comment