当我将整数值分配给 localStorage 项时
localStorage.setItem('a',1)
并检查它的类型
typeof(localStorage.a)
"string"
它返回string,我可以将它转换为int以供我使用
parseInt(localStorage.a)
我的问题是,是否可以在 localStorage 中存储整数值,就像我可以在不进行类型转换的情况下对 Javascript 对象所做的那样?
a={};
a.number=1;
typeof(a.number)
"number"
最佳答案
My question is it possible to store integer value inside localStorage as I can do for Javascript objects without typecasting?
没有。
Storage objects are simple key-value stores, similar to objects, but they stay intact through page loads. The keys can be strings or integers, but the values are always strings. [source]
关于javascript - 是否可以像在 Javascript 对象中那样在 localStorage 中存储整数值并在不进行类型转换的情况下提取它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952287/