1
- var storage = window . localStorage ;
2
-
3
1
function isJSON ( obj ) {
4
2
obj = JSON . stringify ( obj ) ;
5
3
if ( ! / ^ \{ [ \s \S ] * \} $ / . test ( obj ) ) {
@@ -20,21 +18,33 @@ function isFunction(value) { return ({}).toString.call(value) === "[object Funct
20
18
function isArray ( value ) { return Object . prototype . toString . call ( value ) === "[object Array]" ; }
21
19
// https://github.com/jaywcjlove/store.js/pull/8
22
20
// Error: QuotaExceededError
23
- function dealIncognito ( storage ) {
21
+ function dealIncognito ( storage = window . localStorage ) {
24
22
var _KEY = '_Is_Incognit' , _VALUE = 'yes' ;
25
- try { storage . setItem ( _KEY , _VALUE ) }
26
- catch ( e ) {
27
- if ( e . name === 'QuotaExceededError' ) {
28
- var _nothing = function ( ) { } ;
29
- storage . __proto__ = { setItem : _nothing , getItem : _nothing , removeItem : _nothing , clear : _nothing } ;
23
+ try {
24
+ storage . setItem ( _KEY , _VALUE ) ;
25
+ storage . removeItem ( _KEY ) ;
26
+ } catch ( e ) {
27
+ Storage . prototype . _data = { } ;
28
+ Storage . prototype . setItem = function ( id , val ) {
29
+ return this . _data [ id ] = String ( val ) ;
30
+ } ;
31
+ Storage . prototype . getItem = function ( id ) {
32
+ return this . _data . hasOwnProperty ( id ) ? this . _data [ id ] : undefined ;
33
+ } ;
34
+ Storage . prototype . removeItem = function ( id ) {
35
+ return delete this . _data [ id ] ;
36
+ } ;
37
+ Storage . prototype . clear = function ( ) {
38
+ return this . _data = { } ;
30
39
}
40
+ storage = Storage ;
31
41
}
32
42
finally { if ( storage . getItem ( _KEY ) === _VALUE ) storage . removeItem ( _KEY ) ; }
33
43
return storage ;
34
44
}
35
45
36
46
// deal QuotaExceededError if user use incognito mode in browser
37
- storage = dealIncognito ( storage ) ;
47
+ const storage = dealIncognito ( ) ;
38
48
39
49
function Store ( ) {
40
50
if ( ! ( this instanceof Store ) ) {
0 commit comments