Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Storage {
- constructor(name) {
- this.storage_name = name;
- this.storage_local = window.localStorage;
- this.storage_session = window.sessionStorage;
- this.saveLocal = this.saveLocal.bind(this);
- this.saveSession = this.saveSession.bind(this);
- this.getItem = this.getItem.bind(this);
- this.getByLocal = this.getByLocal.bind(this);
- this.getBySession = this.getBySession.bind(this);
- this.clear = this.clear.bind(this);
- }
- saveLocal(obj) {
- this.storage_local.setItem(this.storage_name, JSON.stringify(obj));
- }
- saveSession(obj) {
- this.storage_session.setItem(this.storage_name, JSON.stringify(obj));
- }
- getItem() {
- return this.storage_local.getItem(this.storage_name) ? this.getByLocal() : this.getBySession();
- }
- getByLocal() {
- return JSON.parse(this.storage_local.getItem(this.storage_name));
- }
- getBySession() {
- return JSON.parse(this.storage_session.getItem(this.storage_name));
- }
- clear() {
- this.storage_local.removeItem(this.storage_name);
- this.storage_session.removeItem(this.storage_name);
- }
- }
- export default Storage;
Add Comment
Please, Sign In to add comment