Why localStorage only allows to store string values

栏目: IT技术 · 发布时间: 4年前

内容简介:Take a look at an example:The output is:

localStorage allows data to be stored in browsers across sessions, the data will be there even though the session is expired. It is frequently used to store static data so that they can be loaded when needed. But as per spec, it says that t he keys and the values are  always strings  (note that, as with objects, integer keys will be automatically converted to strings). Why cannot store the object as it is?

Take a look at an example:

var str = "test";
localStorage.setItem("str", str);
console.log(localStorage.getItem("str"));

var obj = {
    a: "hello",
    b: 10
};
localStorage.setItem("obj", obj);
console.log(localStorage.getItem("obj"));

The output is:

test
[object Object]

We can see that the obj is converted to a string. This normally shouldn't be like this as it is what users expect to get in most cases. 

Theoritically it's not an user friendly design to let user implement their own serialization/deserialization logic for the stored object in localStorage. It would make the application logic complicated in big projects. In addition, other storage mechanisms supported in HTML standard do support serialziation/deserialization such as IndexedDB and the obsolete Web SQL.

Indeed localStorage spec in the original HTML draft standard contains the part for serialization/deserialization, however it got removed in the final standard. The Vue.js project contributor 胖茶 di d some research on the change history of localStorage to understand the rationale behind the change. Below are his major findings.

  • In one git change dated back to 2006/04/16 , there were some changes to the signature of getItem() function and there were some mention about serialization/deserialization of E4X, XMLand Arrays in its original comment but there were no detailed explanantion on how to realize them. And the new commit removed those comments.
  • The StructuredSerialize/StructuredDeserialize algorithms implemented in IndexedDB and history.state came way later than the implementation of localStorage. 

So  胖茶 emailed the standar d editor  Ian Hickson about this question and mentioned his findings and guesses and got the response from Ian Hickson.

Yeah that's very plausible. Getting browser vendors on board is a big part of spec writing, so if they were expressing doubts I'd have been eager to simplify for them. There's certainly an argument to be made that having the serialization be done in JS is simpler than having the browser do it, and pretty much just as powerful.

Per what was discussed, the most plausible reason is that it was to reduce the implementation challenge as this localStorage support was added to the spec at a late stage and there was no conclusive decisions on how the serialization/decerialization can be achieved. To unblock the standard and implementation, the support for serialization/deserialization was removed.

Now some browsers like Chrome do support another function which can be used to achieve what we want. It is called localStorage.setObject()/localStorage.getObject().

var obj = {
    a: "hello",
    b: 10
};
localStorage.setObject("obj", obj);
console.log(localStorage.getObject("obj"));

// {a: "hello", b: 10}

However this is not supported in all major browsers(Firefox doesn't support this yet), hence use it with caution.

Reference: https://www.zhihu.com/question/366665675/answer/1127726009


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web表单设计

Web表单设计

[美] Luke Wroblewski / 卢颐、高韵蓓 / 清华大学出版社 / 2010-6 / 49.00元

精心设计的表单,能让用户感到心情舒畅,愉快地注册、付款和进行内容创建和管理,这是促成网上商业成功的秘密武器。本书通过独到、深邃的见解,丰富、真实的实例,道出了表单设计的真谛。新手设计师通过阅读本书,可广泛接触到优秀表单设计的所有构成要素。经验丰富的资深设计师,可深入地了解以前没有注意到的问题及解决方案。 本书专为表单设计或开发人员准备,但同时也适合可用性工程师、网站开发人员、产品经理、视觉设......一起来看看 《Web表单设计》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具