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


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

查看所有标签

猜你喜欢:

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

信息烟尘

信息烟尘

戴维·申克 / 黄锫坚 / 江西教育出版社 / 2002 / 14.50元

今天,我们被大量的信息淹没了:传真、电子邮件、各种新闻、消息和铺天盖地的广告,正如人们以前预示的那样:出现了一个令人鼓舞的信息时代,媒体专家兼网络评论员戴维·申克透过这些繁荣的表象,揭示了大量的无用的信息对我们造成的干扰,或者说,“信息烟尘”对我们个人的健康(包括精神上的和肉体上的)及对社会造成的极大危害。这《信息烟尘:在信息爆炸中求生存》宣告了“信息时代”神话的破灭。一起来看看 《信息烟尘》 这本书的介绍吧!

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

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具