使用localStorage存储没有JSON的对象

时间:2016-03-17 21:12:53

标签: javascript jquery html5

我需要能够使用localStorage或任何真正有用的东西来保存用户的信息。我不太了解localStorage,但我认为为了在其中存储一个对象,我需要在对象制作者中使用一个函数。也许我错了,但这就是我发帖的原因。

我认为您可以使用localStorage.username(this.username)来存储用户名,但我现在需要如何在不使用JSON stringify的情况下存储整个对象。 localStorage.newPlayer()

JSfiddle link here

function Player(username, lvl, exp, expNeeded, nextLevel, gold, hp, atk, def, spd) {
    var self = this;
    this.username = username;
    this.lvl = lvl;
    this.exp = exp;
    this.nextLevel = nextLevel;
    this.expNeeded = expNeeded;
    this.gold = gold;
    this.fullLife = hp;
    this.hp = hp;
    this.atk = atk;
    this.def = def;
    this.spd = spd;
    this.implement = function() {
        $('#user').text(this.username).addClass('playerName').data('player', self);
        $('#username').text("Username: " + this.username);
        $('#lvl').text("Level: " + this.lvl);
        $('#exp').text("Experience: " + this.exp);
        $('#expNeeded').text("Experience Needed: " + this.expNeeded);
        $('#gold').text("Gold: " + this.gold);
        $('#hp').text("HP: " + this.fullLife);
        $('#attack').text("Attack: " + this.atk);
        $('#defense').text("Defense: " + this.def);
        $('#speed').text("Speed: " + this.spd);
        $('#nextLevel').text("Next Level: " + this.nextLevel);
    };
    this.implement();
}
if($('body').attr("id") == "Home") {
    var newPlayer = new Player(prompt("What is your username?"), 1, 0, 10, 10, 0, 10, 2, 1, 1);
}
playerEl = $('.playerName');
player = playerEl.data('player');

如果您需要查看完整代码,请告诉我

2 个答案:

答案 0 :(得分:1)

你能解释为什么JSON.stringify(new Player())不被接受吗?

如果您不能使用JSON,则只需将每个名称/值对分别存储到localStorage中并单独取出或构建自己的自定义字符串以放置在{{1}中},但localStorage真的最适合你的场景。

我认为你只需要从播放器功能中删除它:

JSON.stringify()

这样可以将播放器序列化为JSON对象,然后可以将其设置为localStorage。

然后,该代码将被移动到您的this.implement = function() { $('#user').text(this.username).addClass('playerName').data('player', self); $('#username').text("Username: " + this.username); $('#lvl').text("Level: " + this.lvl); $('#exp').text("Experience: " + this.exp); $('#expNeeded').text("Experience Needed: " + this.expNeeded); $('#gold').text("Gold: " + this.gold); $('#hp').text("HP: " + this.fullLife); $('#attack').text("Attack: " + this.atk); $('#defense').text("Defense: " + this.def); $('#speed').text("Speed: " + this.spd); $('#nextLevel').text("Next Level: " + this.nextLevel); }; this.implement(); 语句的真正块中,对if的引用将被您的this对象实例替换。

看到这个工作小提琴:https://jsfiddle.net/LqgLxr0r/5/

enter image description here

答案 1 :(得分:-1)

//global function

Storage.prototype.setObject = function(key, value) {
    this.setItem(key, JSON.stringify(value));
  }

  Storage.prototype.getObject = function(key) {
    var value = this.getItem(key);
    return value && JSON.parse(value);
  }


//page 1 code 

//your data
 var arrayObj = [{
    name: 'user4',
    userid: '1111',
    msg: 'hello world!'
  }, {
    name: 'user2',
    userid: '2222',
    msg: 'hello universe!'
  }];







    localStorage.setObject('userInfo', arrayObj);

// page 1 code ends here


// page 2 code

    if (window.localStorage) {

        var $val = localStorage.getObject('userInfo', arrayObj);
    }

// returns $val =   [{name: 'user4',userid: '1111',msg: 'hello world!'}, {name: 'user2',userid: '2222',msg: 'hello universe!'}];