localStorage - 直接使用getItem / setItem函数或访问对象? [英] localStorage - use getItem/setItem functions or access object directly?

查看:314
本文介绍了localStorage - 直接使用getItem / setItem函数或访问对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用localStorage对象上定义的方法与直接访问对象属性有什么好处?例如,而不是:

  var x = localStorage.getItem(key); 
localStorage.setItem(key,data);

我一直这样做:

  var x = localStorage [key]; 
localStorage [key] = data;

这有什么问题吗?

解决方案

不是真的,它们基本上是完全一样的。一个使用封装(getter / setter)来更好地保护数据和简单使用。您应该使用此样式(为了安全性)。



另一个允许在名称(键)未知以及数组和循环时更好地使用。使用 .key() .length 在不知道实际密钥名称的情况下迭代存储项目。



我发现这是一个很好的资源: http:// diveintohtml5 .info / storage.html



此问题可能会为某些人提供更多见解: HTML5 localStorage密钥订单



附录:



<很明显,封装存在一些混乱。查看这个快速维基百科。但严重的是,我希望这个网站的用户知道如何谷歌。



继续前进,封装是一个想法,你进出门户网站与另一个进行通信系统。假设您正在制作 API 包供其他人使用。假设您在该API系统中有一系列信息,可通过用户输入进行更新。您可以使用 array [key] 方法将API中的用户直接将该信息放入数组中。或者你可以使用封装。获取将其添加到数组中的代码并将其包装在函数中(例如, setArray() setWhateverMakesSense()函数)API的用户调用以添加此类信息。然后,在这个 set 函数中,您可以检查数据是否存在问题,您可以以正确的方式将其添加到数组中,以备不时之需以某种方式推送 ed或 shift ed到数组...等。 控制用户输入如何进入实际程序。因此,它本身不会增加安全性,但允许您(API的作者)编写安全性。这也允许更好的版本控制/更新,因为如果您决定进行内部更改,API的用户将不必重写代码。但无论如何,这是良好的 OOP 所固有的。



在javascript和 localStorage 对象的情况下,他们已经编写了这个API,他们是作者,我们是它的用户。如果作者决定改变 localStorage 的工作方式,那么如果使用封装方法,则不太可能重写代码。但我们都知道这种变化水平不太可能发生,至少不会很快发生。既然作者在这里没有任何固有的不同的安全检查,那么,目前,这两种使用 localStorage 的方式基本上都是相同。它有点像垫片。但是,我们可以轻松地使用自己的封装来覆盖/替换 localStorage 周围的现有封装,以进行我们自己的安全检查。



PT


Are there some benefits of using the methods defined on the localStorage object versus accessing the object properties directly? For example, instead of:

var x = localStorage.getItem(key);
localStorage.setItem(key, data);

I have been doing this:

var x = localStorage[key];
localStorage[key] = data;

Is there anything wrong with this?

解决方案

Not really, they are, basically, exactly the same. One uses encapsulation (getter/setter) to better protect the data and for simple usage. You're supposed to use this style (for security).

The other allows for better usage when names(keys) are unknown and for arrays and loops. Use .key() and .length to iterate through your storage items without knowing their actual key names.

I found this to be a great resource : http://diveintohtml5.info/storage.html

This question might provide more insight as well to some: HTML5 localStorage key order

Addendum:

Clearly there has been some confusion about encapsulation. Check out this quick Wikipedia. But seriously, I would hope users of this site know how to google.

Moving on, encapsulation is the idea that you are making little in and out portals for communication with another system. Say you are making an API package for others to use. Say you have an array of information in that API system that gets updated by user input. You could make users of your API directly put that information in the array... using the array[key] method. OR you could use encapsulation. Take the code that adds it to the array and wrap it in a function (say, a setArray() or setWhateverMakesSense() function) that the user of your API calls to add this type of information. Then, in this set function you can check the data for issues, you can add it to the array in the correct way, in case you need it pushed or shifted onto the array in a certain way...etc. you control how the input from the user gets into the actual program. So, by itself it does not add security, but allows for security to be written by you, the author of the API. This also allows for better versioning/updating as users of your API will not have to rewrite code if you decide to make internal changes. But this is inherent to good OOP anyhow.

In the case here of javascript and the localStorage object, they have already written this API, they are the author, and we are its users. If the authors decide to change how localStorage works, then it will be less likely to have to rewrite your code if the encapsulation methods were used. But we all know its highly unlikely that this level of change will ever happen, at least not any time soon. And since the authors didn't have any inherent different safety checks to make here, then, currently, both these ways of using localStorage are essentially the same. It's kind of like a shim. However, we can easily make our own encapsulation to overwrite/replace the existing encapsulation around localStorage to make our own security checks.

PT

这篇关于localStorage - 直接使用getItem / setItem函数或访问对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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