Skip to content

Instantly share code, notes, and snippets.

@romanzhivo
Last active November 27, 2020 18:15
Show Gist options
  • Save romanzhivo/657c74240801881241cfa64dc3429821 to your computer and use it in GitHub Desktop.
Save romanzhivo/657c74240801881241cfa64dc3429821 to your computer and use it in GitHub Desktop.
Модуль-класс с реализацией методов взаимодействия с локальным хранилищем (localStorage)
const localStorage = window.localStorage || {};
class LStorage {
constructor() {
const self = this;
self.setData = self.setData;
self.getData = self.getData;
self.removeData = self.removeData;
self.clearData = self.clearData;
}
setData(key, data) {
if(localStorage && localStorage.setItem) {
if(JSON.stringify(data) === undefined)
localStorage.setItem(key, JSON.stringify("" + data + ""));
else
localStorage.setItem(key, JSON.stringify(data));
}
}
getData(key) {
let status;
if(localStorage && localStorage.getItem) {
status = localStorage.getItem(key);
if(!status)
return undefined;
else
return JSON.parse(localStorage.getItem(key));
}
};
removeData(key) {
localStorage.removeItem(key);
};
clearData() {
localStorage.clear();
};
}
export default LStorage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment