Created
July 24, 2012 04:16
-
-
Save sofish/3168021 to your computer and use it in GitHub Desktop.
localstorage 不能直接包
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>LocalStorage fault</title> | |
<link rel="stylesheet" href="http://typo.sofish.de/typo.css" /> | |
</head> | |
<body class="typo" style="width:800px;margin:30px auto;"> | |
<h3># error message</h3> | |
<blockquote> | |
NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object | |
</blockquote> | |
<h3># origin code:</h3> | |
<pre> | |
var obj = (function(w){ | |
return { | |
set: localStorage.setItem, | |
get: localStorage.getItem, | |
clear: localStorage.clear | |
} | |
})(window); | |
obj.set('key', 'the value'); | |
</pre> | |
<h3># solution:</h3> | |
<pre> | |
var obj = (function(w){ | |
return { | |
set: function(key, value) { | |
return localStorage.setItem(key, value) | |
}, | |
get: function(key){ | |
return localStorage.getItem(key) | |
} | |
clear: function(){ | |
return localStorage.clear() | |
} | |
} | |
})(window); | |
obj.set('key', 'the value'); | |
</pre> | |
<script> | |
var obj = (function(w){ | |
return { | |
set: localStorage.setItem, | |
get: localStorage.getItem, | |
clear: localStorage.clear | |
} | |
})(window); | |
obj.set('key', 'the value'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment