Why can't I extend localStorage on IE8 (javascript)?

自作多情 提交于 2019-12-31 01:56:05

问题


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);

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]);
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/10514705/why-cant-i-extend-localstorage-on-ie8-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!