Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
localstorage 不能直接包
<!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
You can’t perform that action at this time.