将对象权限设置为localStorage

时间:2014-08-30 19:56:58

标签: javascript

我有一个更多礼仪的obj。如何将其中一个属性设置为本地存储。只是适当的不是洞Obj(在java中)。这是我尝试过的东西:

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);

1 个答案:

答案 0 :(得分: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
}