datastorage.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. function initStorage(obj, debug = false)
  2. {
  3. var dataStorage =
  4. {
  5. clone: function(key)
  6. {
  7. if(typeof this['_' + key] != 'object') return this['_' + key]
  8. return JSON.parse(JSON.stringify(this['_' + key]));
  9. },
  10. addProperty: function(key, value)
  11. {
  12. this[key] =
  13. {
  14. get: function()
  15. {
  16. if(debug)
  17. {
  18. console.groupCollapsed('Get %c' + key, 'color: #3785ff');
  19. console.log(this['_' + key]);
  20. console.trace();
  21. console.groupEnd();
  22. }
  23. return this['_' + key];
  24. },
  25. set: function(value)
  26. {
  27. if(debug)
  28. {
  29. console.groupCollapsed('Set %c' + key, 'color: #ff6970');
  30. console.log('Before', this['_' + key]);
  31. console.log('Value', value);
  32. console.trace();
  33. console.groupEnd();
  34. }
  35. this['_' + key] = value;
  36. }
  37. }
  38. }
  39. };
  40. for(let key in obj)
  41. {
  42. dataStorage['_' + key] = obj[key];
  43. Object.defineProperty(dataStorage, key,
  44. {
  45. get: function()
  46. {
  47. if(debug)
  48. {
  49. console.groupCollapsed('Get %c' + key, 'color: #3785ff');
  50. console.log(this.clone(key));
  51. console.trace();
  52. console.groupEnd();
  53. }
  54. return this.clone(key);
  55. },
  56. set: function(value)
  57. {
  58. if(debug)
  59. {
  60. console.groupCollapsed('Set %c' + key, 'color: #ff6970');
  61. console.log('Before', this.clone(key));
  62. console.log('Value', value);
  63. console.trace();
  64. console.groupEnd();
  65. }
  66. this['_' + key] = value;
  67. }
  68. });
  69. }
  70. return dataStorage;
  71. }