2014-08-30 18 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 
}