Created
October 28, 2015 15:50
-
-
Save atomrc/c22f699b898ef4dff7a6 to your computer and use it in GitHub Desktop.
Stupid fallback for localStorage when browser doesn't support it
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
/** | |
* Warning : only use this piece of code | |
* if storing things in the localStorage is | |
* NOT vital for your application to work | |
**/ | |
/* function noop | |
* simply does nothing | |
**/ | |
function noop() {} | |
/** | |
* if window.localStorage is defined then `storage` | |
* will contain it. Else `storage` will simply be a | |
* stupid mock for localStorage and won't store anything | |
*/ | |
var storage = window.localStorage || { | |
length: 0, | |
key: noop, | |
setItem: noop, | |
getItem: noop, | |
removeItem: noop, | |
clear: noop | |
}; | |
/** | |
* Then you can use `storage` like you would use `window.localStorage` | |
* without worrying about browser compatibility | |
* / | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Thomas, agree with you on that :(
btw, for avoiding to declare an empty function, you can use directly
Function.prototype
:)