localStorageã¨ãsessionStorageã¨ãã®ã¢ã¬
ç¾ç¶ã®localStorageã¨ãsessionStorageã¯ãvalueã¨ãã¦æååããsetã§ããªãå®è£ ã°ãããªã®ã§ãã¢ããªã§ä½¿ããªãsetããæã«JSON.stringifyãã¦ãgetããæã«JSON.parseãããããªã©ããã¼ãæ¸ããªãã¨ç¡çã¨ã¾ã§ã¯è¨ããªããããã©ãããã
ã¾ãã
localStorage.key = "hoge"; localStorage[key] = "hoge"; localStorage.setItem(key, "hoge");
ã¯ãä¸å¿ç®çã¨ãã¦ã¯åããªã®ã ããsetItemãgetItemã¯ã©ããããã©ã¦ã¶ãã¨ã«æåãå¾®å¦ã«ç°ãªãã¿ããã
ä¾ãã°ãæå ã®Firefox4.0.1ã§ã¯ãsetItem(null, "foo"); ãå®è¡ãã¦ãnullããã¼ã¨ãã¦"foo"ãsetããããã¨ã¯ãªãããsetItem(undefined, "foo"); ãå®è¡ããã¨undefinedãkeyã¨ãã¦ã"foo"ã¨ããvalueãæ ¼ç´ããã¦ãã¾ãã
ä¸æ¹æå ã®Chrome 11.0.696.14 devã®localStorage.setItemã¯ãnullãundefinedãkeyã¨ãã¦ããããé ãã¦ãã¾ãããã ã
ãããã£ãsetItemããgetItemã®æåã«ä¾åããã³ã¼ããæ¸ãã¦ã¯ãããªãã
ã¾ãã
localStorage.setItem({}, "obj");
ãå®è¡ããã¨ã"[object Object]"ã¨ããæååãkeyã¨ãã¦ã"obj"ãvalueã¨ãã¦ä¿åãããã
ããã¯Firefoxã§ãChromeã§ãtoStringãè¦ã¦ãããããªã®ã§
function Hoge() { } Hoge.prototype.toString = function() { return "[homuhomu]"; }; var o = new Hoge(); localStorage.setItem(o, "obj");
ã¨ãããã¨ããã§ãã"[homuhomu]"ãkeyã¨ãã¦"obj"ãä¿åããããã¨ã«ãªãã
null, undefined, ãªãã¸ã§ã¯ãã®ãããã«ã¤ãã¦ãã¦ã¼ã¶å´ãæå¾ ãããããªåä½ã§ã¯ãªãã®ã§ãã©ããã¼ã§ãã§ãã¯ãã¦æ°åã¨æåå以å¤ãkeyã¨ãã¦æ¸¡ããã¨ãããããã¡ãã£ãæ¹ããããã¨ãªã¢ã¯ã¬ã«ï¼ãã®è¾ºã®ããªã·ã¼ã¯ã©ã決ãããã®åé¡ã ãã©ï¼ã