有问题。我这样创建我的游戏对象:
var game_status = {
money : 350000000,
reset_status : function() {
this.money = 350000000;
}
}
function saveGame(){
localStorage.setItem('game_status', JSON.stringify(game_status));
}
function loadGame(){
game_status = JSON.parse(localStorage.getItem('game_status'));
}
加载游戏状态方法“reset_status”后不再存在。
也许我应该描述对象、创建实例、保存实例,然后使用加载的参数重新加载创建实例?
最佳答案
只需将存储的状态分配给对象:
Object.assign(game_status, JSON.parse(localStorage.getItem('game_status')));
这样你就不必编写构造函数。
关于javascript - 如何使用方法保存和加载对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51466926/