Last active
November 27, 2020 18:15
-
-
Save romanzhivo/657c74240801881241cfa64dc3429821 to your computer and use it in GitHub Desktop.
Модуль-класс с реализацией методов взаимодействия с локальным хранилищем (localStorage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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