判断浏览器是否支持localStorage

  1. function isLocalStorageUsable(){
  2. const localStorageTestKey = '_localStorage_support_test';
  3. const localStorageTestValue = 'test';
  4. let isSupport = false;
  5. try{
  6. localStorage.setItem(localStorageTestKey,localStorageTestValue)
  7. if(localStorage.getItem(localStorageTestKey) === localStorageTestValue){
  8. isSupport = true;
  9. }
  10. localStorage.removeItem(localStorageTestKey);
  11. return isSupport;
  12. }catch(e){
  13. if(e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED'){
  14. console.log('localStorage 存储已达上限!')
  15. }else{
  16. console.warn('当前浏览器不支持 localStorage!')
  17. }
  18. }
  19. return isSupport
  20. }

避免多次调用该函数

  1. ready(){
  2. if(isSupport === null){
  3. isSupport = isLocalStorageUsable()
  4. }
  5. if(isSupport) {
  6. return Promise.resolve()
  7. }
  8. return Promise.reject()
  9. }

处理key为对象的情况

如果可以的值是对象,那么会隐式的调用 toString() 方法。这会导致key的值都变成 [object Object],容易造成key重复
避免key是对象而覆盖其他值

  1. function normalizeKey(key){
  2. if(typeof key !== 'string'){
  3. console.warn(`${key} used as a key, but it is not a string`);
  4. key = String(key);
  5. }
  6. return key
  7. }

将value进行序列化

  1. function serialize(value, callback) {
  2. try{
  3. const valueString = JSON.stringify(value);
  4. callback( null, valueString);
  5. }catch(e) {
  6. console.log(e);
  7. }
  8. }

整体调用

  1. setItem(key, value){
  2. key = normalizeKey(key);
  3. return ready().then(()=>{
  4. if (value === undefined) {
  5. value = null;
  6. }
  7. serialize(value, (error, valueString)=>{
  8. if (error) {
  9. return Promise.rehect(error);
  10. }
  11. try{
  12. localStorage.setItem(key, valueString);
  13. return Promise.resolve();
  14. } catch(e) {
  15. return Promise.reject(e)
  16. }
  17. })
  18. })
  19. }

组件封装

  1. let isSupport = null
  2. function isLocalStorageUsable(){
  3. const localStorageTestKey = '_localStorage_support_test';
  4. const localStorageTestValue = 'test';
  5. let isSupport = false;
  6. try{
  7. localStorage.setItem(localStorageTestKey,localStorageTestValue)
  8. if(localStorage.getItem(localStorageTestKey) === localStorageTestValue){
  9. isSupport = true;
  10. }
  11. localStorage.removeItem(localStorageTestKey);
  12. return isSupport;
  13. }catch(e){
  14. if(e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED'){
  15. console.log('localStorage 存储已达上限!')
  16. }else{
  17. console.warn('当前浏览器不支持 localStorage!')
  18. }
  19. }
  20. return isSupport
  21. }
  22. function serialize(value, callback) {
  23. try{
  24. const valueString = JSON.stringify(value);
  25. callback( null, valueString);
  26. }catch(e) {
  27. console.log(e);
  28. }
  29. }
  30. function normalizeKey(key){
  31. if(typeof key !== 'string'){
  32. console.warn(`${key} used as a key, but it is not a string`);
  33. key = String(key);
  34. }
  35. return key
  36. }
  37. function ready(){
  38. if(isSupport === null){
  39. isSupport = isLocalStorageUsable()
  40. }
  41. if(isSupport) {
  42. return Promise.resolve()
  43. }
  44. return Promise.reject()
  45. }
  46. function setItem(key, value){
  47. key = normalizeKey(key);
  48. return ready().then(()=>{
  49. if (value === undefined) {
  50. value = null;
  51. }
  52. serialize(value, (error, valueString)=>{
  53. if (error) {
  54. return Promise.rehect(error);
  55. }
  56. try{
  57. localStorage.setItem(key, valueString);
  58. return Promise.resolve();
  59. } catch(e) {
  60. return Promise.reject(e)
  61. }
  62. })
  63. })
  64. }
  65. export default setItem