為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)


問題描述

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

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.

(by Eric Johnsonkirilloid)

參考文件

  1. Why can't I extend localStorage on IE8 (javascript)? (CC BY-SA 3.0/4.0)

#local-storage #javascript






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

當我有 <!DOCTYPE html> 時沒有 userData 行為? (No userData behavior when I have <!DOCTYPE html>?)

Bisakah Penyimpanan lokal HTML5 di aplikasi Cordova/Phonegap disinkronkan ke iCloud? (Can HTML5 localStorage in Cordova/Phonegap app be synced to iCloud?)

存儲限制(在 IndexedDB 或 localStorage 上)是否適用於在計算機外運行的本地應用程序? (Does the storage limit (on IndexedDB or localStorage) apply for local apps running off the computer?)

不在 sencha 的本地存儲中存儲價值 (Not storing value on localstorage in sencha)

如何在後台頁面中使用本地存儲打開/關閉 chrome 擴展? (How to turn on/off chrome extension using local storage in background page?)

localstorage observable 服務上的主題切換器 (Theme switcher on localstorage observable service)

要更新我需要重新加載頁面的數據,我該如何避免這種情況? (to update the data I need to reload the page, how can I avoid this?)

javascript - 使用本地存儲的 NOT vs NULL (javascript - NOT vs NULL with Local storage)

我想將我的電子商務應用的購物車詳細信息保存到本地存儲中 (I want to save cart details of my Ecommerce app into local storage)

我通過表單發送的數據不顯示(localStorage) (the data i send through the form does not display(localStorage))

本地存儲未正確更新陣列 (local storage not updating array properly)







留言討論