Guest User

Untitled

a guest
Nov 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. class Storage {
  2. constructor(name) {
  3. this.storage_name = name;
  4. this.storage_local = window.localStorage;
  5. this.storage_session = window.sessionStorage;
  6.  
  7. this.saveLocal = this.saveLocal.bind(this);
  8. this.saveSession = this.saveSession.bind(this);
  9.  
  10. this.getItem = this.getItem.bind(this);
  11. this.getByLocal = this.getByLocal.bind(this);
  12. this.getBySession = this.getBySession.bind(this);
  13.  
  14. this.clear = this.clear.bind(this);
  15. }
  16.  
  17. saveLocal(obj) {
  18. this.storage_local.setItem(this.storage_name, JSON.stringify(obj));
  19. }
  20.  
  21. saveSession(obj) {
  22. this.storage_session.setItem(this.storage_name, JSON.stringify(obj));
  23. }
  24.  
  25. getItem() {
  26. return this.storage_local.getItem(this.storage_name) ? this.getByLocal() : this.getBySession();
  27. }
  28.  
  29. getByLocal() {
  30. return JSON.parse(this.storage_local.getItem(this.storage_name));
  31. }
  32.  
  33. getBySession() {
  34. return JSON.parse(this.storage_session.getItem(this.storage_name));
  35. }
  36.  
  37. clear() {
  38. this.storage_local.removeItem(this.storage_name);
  39. this.storage_session.removeItem(this.storage_name);
  40. }
  41. }
  42.  
  43. export default Storage;
Add Comment
Please, Sign In to add comment