Skip to content

Instantly share code, notes, and snippets.

@sofish
Created July 24, 2012 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sofish/3168021 to your computer and use it in GitHub Desktop.
Save sofish/3168021 to your computer and use it in GitHub Desktop.
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