PaulPaulAga

Untitled

May 20th, 2021
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. нужно реализовать паттерн chaining
  3.  
  4. написать функцию createHash
  5.  
  6. которая сможет работать вот так:
  7.  
  8. createHash()
  9.   .set('3', 2)
  10.   .set('4', 5)
  11.   .remove('3')
  12.   .getValues() // {'4': 5}
  13. */
  14.  
  15. class Hash {
  16.     #obj
  17.  
  18.     constructor() {
  19.         this.#obj = {};
  20.     }
  21.  
  22.     set(key, value) {
  23.         this.#obj[key] = value;
  24.         return this;
  25.     }
  26.  
  27.     remove(key) {
  28.         delete this.#obj[key];
  29.         return this;
  30.     }
  31.  
  32.     getValues() {
  33.         return this.#obj;
  34.     }
  35. };
  36.  
  37. createHash = () => {
  38.     return new Hash();
  39. }
  40.  
  41. hash = createHash().set('3', 2).set('4', 5).remove('3').set('3', 10);
  42.  
  43. console.log(hash.getValues());
  44.  
  45. hash.obj = {anyValue: 4};
  46.  
  47. console.log(hash.getValues());
Advertisement
Add Comment
Please, Sign In to add comment