2014-08-30 19 views
0

我有更多礼仪的对象。我如何设置本地存储的其中一个特性?只是适当而不是洞Obj(在java中)。下面是一些我想:将物件设定为localStorage

function game(){ 
     this.team_one = "team_one"; 
     this.team_two = "team_two"; 
     this.score_home function(val){ 
     return(typeof(val) === 'undefined' ? : localStorage.myVal = val); 
     }; 
     this.score_away = 0; 
} 
for(var i=0;i<30;i++){ 
    game_on.push(new game()); 
} 
game_on[0].score_home(7); 
alert(game_on[0].score_home); 
+0

1)java的! = JavaScript的; 2)你需要检索你的对象并修改你的属性...并且更新localStorage – HellBaby 2014-08-30 20:06:43

回答

0

将数据存储在localStorage的,你可以使用:

localStorage.setItem(property_name, property_value); //to add property to localStorage 
var myProperty = localStorage.getItem(property_name); // to retrieve value. 

这也是更好地检查,如果浏览器支持的localStorage:

if(typeof(Storage) !== "undefined") { 
    // Browser supports localStorage and sessionStorage 
} else { 
    // Browser does not support any of them 
}