Batisk_AFF

xmap-js-rubaxa

Nov 30th, 2021 (edited)
445
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Реализовать свой Map
  3.  * @constructor
  4.  */
  5. function XMap() {
  6.     const buf = Object.create(null);
  7.    
  8.     this.set = (key, value) => {
  9.         buf[key] = value;
  10.     };
  11.    
  12.     this.get = (key) => {
  13.         return buf[key];
  14.     };
  15.    
  16.     this.has = (key) => {
  17.         return buf[key] !== undefined;
  18.     };
  19.    
  20.     this.remove = (key) => {
  21.         delete buf[key];
  22.     };
  23. }
  24.  
  25. // Проверка
  26. const map = new XMap();
  27. const objKey = {foo: true};
  28.  
  29. map.set(123, 'ok');
  30. map.set(objKey, 'fail');
  31. map.has(objKey) && map.set(objKey, 'wow');
  32.  
  33. console.log(map.get(123)); // "ok"
  34. console.log(map.get(objKey)); // "wow"
  35.  
  36. map.remove(123);
  37. console.log(map.has(123)); // false
  38.  
  39. map.has('toString');
RAW Paste Data