是否有可能像JavaScript对象一样在localStorage中存储整数值,并提取它没有类型转换? [英] Is it possible to store integer value in localStorage like in Javascript objects and extract it without typecasting?

查看:318
本文介绍了是否有可能像JavaScript对象一样在localStorage中存储整数值,并提取它没有类型转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将整数值分配给localStorage项目时

When I assign integer value to localStorage item

localStorage.setItem('a',1)

并检查其类型

and check its type

typeof(localStorage.a)
"string"

返回字符串,我可以将它强制转换为 int 供我使用

it returns string, I can typecast it to int for my use

parseInt(localStorage.a)

我的问题是可以在localStorage中存储整数值,因为我可以为没有类型转换的Javascript对象做?

My question is it possible to store integer value inside localStorage as I can do for Javascript objects without typecasting?

a={};
a.number=1;
typeof(a.number)
"number"


推荐答案


我的问题是可以在localStorage中存储整数值,因为我可以为Javascript对象而不进行类型转换吗?

My question is it possible to store integer value inside localStorage as I can do for Javascript objects without typecasting?

否。


存储对象是简单的键值存储,类似于对象,但它们通过页面保持不变负载。键可以是字符串或整数,但值始终为字符串 [source]

这篇关于是否有可能像JavaScript对象一样在localStorage中存储整数值,并提取它没有类型转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆