英文:
is this method of saving numbers or booleans in a simple offline web game okay or should I stop?
问题
To save numbers and booleans, I use array destructuring. I simply save by localStorage.save=JSON.stringify([stat1, stat2, stat3]);
then I load by [stat1, stat2, stat3] = JSON.parse(localStorage.save);
I just want to know if this is bad, and I should stop or if it is okay. By the way, strings aren't included in this method unless you do '"' + stringvar + '"'
in the save code, which is the only thing I noticed.
英文:
to save numbers and booleans I use array destructuring. I simply save by
localStorage.save=JSON.stringify([stat1, stat2, stat3]);
then I load by
[stat1, stat2, stat3] = JSON.parse(localStorage.save);
I just want to know if this is bad, and I should stop or if it is okay.
by the way strings aren't included in this method unless you do '"'+stringvar+'"' in the save code which is the only thing i noticed
答案1
得分: 0
Saving data to the local storage is fine (but keep in mind most browsers have a quota, but a few numbers certainly won't hit that limit)
Edit: Thanks to the comment of @Yogi I learned the shortcut for using localStorage.foo = '...'
but I cannot delete this answer anymore because it's already accepted ...
but your localStorage.save = ...
doesn't make any sense and doesn't save anything to the local storage. It just sets a custom property of the localStorage object. This may work as long as you don't reload your site (as everything is kept in memory). But once you reload your site, your data is gone.
The correct way would be
localStorage.setItem("gameStats", JSON.stringify([stat1, stat2, stat3]))
and
let [stat1, stat2, stat3] = JSON.parse(localStorage.getItem("gameStats"));
英文:
Saving data to the local storage is fine (but keep in mind most browsers have a quota, but a few numbers certainly won't hit that limit)
Edit: Thanks to the comment of @Yogi I learned the shortcut for using localStorage.foo = '...'
but I cannot delete this answer anymore because it's already accepted ...
<strike>but your localStorage.save = ...
doesn't make any sense and doesn't save anything to the local storage. It just sets a custom property of the localStorage object. This may work as long as you don't reload your site (as everything is kept in memory). But once you reload your site, your data is gone.
The correct way would be
localStorage.setItem("gameStats", JSON.stringify([stat1, stat2, stat3]))
and
let [stat1, stat2, stat3] = JSON.parse(localStorage.getItem("gameStats"));
</strike>
答案2
得分: 0
保存到localStorage
是完全可以的,唯一的问题是你应该在命名方面更加描述性,因为如果有人查看你存储的数据,无法推断出什么意思。
此外,建议使用localStorage::setItem()
来替代直接给localStorage
分配属性。
由于你使用JSON序列化数据,我猜字符串和对象都会被序列化得很好。
因此,我建议:
// 保存(您将保存一个具有描述性对象属性名称的良好对象
// 'gameData' - 这可以扩展到统计数据以外的其他数据
localStorage.setItem('gameData', JSON.stringify({stat1, stat2, stat3});
// 恢复
const {stat1, stat2, stat3} = JSON.parse(localStorage.getItem('gameData'));
英文:
Saving to localStorage
is totally ok, the only problem you should be more descriptive with your naming, because there's nothing deducible if one looks at your stored data.
Also it's recommended to use localStorage::setItem()
instead of assigning properties to localStorage
itself.
Since you serialize the data with JSON my guess strings will be serialized just ok as well as even objects.
So I propose:
// saving (you will save a nice object with descriptive object property names
// 'gameData' - this is extendable with other data beyond stats
localStorage.setItem('gameData', JSON.stringify({stat1, stat2, stat3});
// restoring
const {stat1, stat2, stat3} = JSON.parse(localStorage.getItem('gameData'));
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论