1. 程式人生 > >localStorage 儲存物件Object

localStorage 儲存物件Object

localStorage 只能儲存字串,如果直接setItem 一個物件,則會執行Object 的toString() 方法,儲存其結果

    var testObj = {
        "userName": "xixi",
        toString: function () {
            return "userName:'xixi'";
        }
    };
    localStorage.setItem("myobject", testObj); 
    alert(localStorage.getItem("myobject"));