为什么我不能在IE8(javascript)上扩展localStorage? [英] Why can't I extend localStorage on IE8 (javascript)?
问题描述
我想向localStorage添加2个方法。我的目标是最终得到这样的结果:
I'd like to add 2 methods to localStorage. My goal is to end up with something like this:
localStorage.setObject(key, object);
localStorage.getObject(key);
此解决方案适用于大多数浏览器,但不适用于IE8:
This solution works in most browsers but not IE8:
Storage.prototype.setObject = function(key, value) {
this[key] = JSON.stringify(value);
}
Storage.prototype.getObject = function(key) {
return JSON.parse(this[key]);
}
经过一些研究,显然我可以使用Lawnchair.js或解决它另一种方式。但我想知道为什么它在IE8中不起作用。我可以扩展String和Array。为什么不存储?如何找出哪些对象可以扩展以及哪些对象无法在IE8中扩展?
After doing some research, apparently I could use Lawnchair.js or work around it some other way. But I'm wondering why it doesn't work in IE8. I can extend String and Array. Why not Storage? How can I find out which objects I can extend and which ones I can't extend in IE8?
推荐答案
这是 IE 即可。您也无法扩展DOM元素。如果你有时真的需要调用一个函数,你可以通过 Storage.prototype.getObject.call(localStorage,'hello')
来完成。
This is IE. You also can't extend DOM elements. If you sometimes really-really need to call a function, you can do it via Storage.prototype.getObject.call(localStorage, 'hello')
.
同样扩展内置对象并不是件好事。
Also extending built-in objects is not considered as good thing.
这篇关于为什么我不能在IE8(javascript)上扩展localStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!