is this method of saving numbers or booleans in a simple offline web game okay or should I stop?

huangapple go评论72阅读模式
英文:

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(&quot;gameStats&quot;, JSON.stringify([stat1, stat2, stat3]))

and

let [stat1, stat2, stat3] = JSON.parse(localStorage.getItem(&quot;gameStats&quot;));

</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
// &#39;gameData&#39; - this is extendable with other data beyond stats
localStorage.setItem(&#39;gameData&#39;, JSON.stringify({stat1, stat2, stat3}); 

// restoring
const {stat1, stat2, stat3} = JSON.parse(localStorage.getItem(&#39;gameData&#39;));

</details>



huangapple
  • 本文由 发表于 2023年5月21日 14:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定