javascript localStorage - clear() 还是 removeItem()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15486484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
localStorage - clear() or removeItem()?
提问by Scott
Should I use clear()
to obliterate everything in localStorage
, or should I just manually removeItem()
the ones I've set on that particular site (which is easy enough to keep track of)?
我应该使用clear()
在抹杀一切localStorage
,或者我应该只是手工removeItem()
的那些我已经在那个特定的网站(这是很容易够跟踪)设置?
I ask because I don't want to end up wiping out users' localStorage
if they have other values set. I'm testing this in localhost and noticed that by using clear()
, everything I'd set previously in other projects was wiped out.
我问是因为我不想最终消灭用户,localStorage
如果他们设置了其他值。我正在 localhost 中对此进行测试,并注意到通过使用clear()
,我之前在其他项目中设置的所有内容都被清除了。
EDIT: I should have mentioned that I know localStorage is domain-locked. I'm running a site that follows this structure:
编辑:我应该提到我知道 localStorage 是域锁定的。我正在运行一个遵循以下结构的站点:
public-html
(localStorage)
--project1
----files
--project2
----files
--project3
----files
Where each file uses it's own separate localStorage variables. If I localstorage.clear()
inside project2, project1 and project3's settings will be lost as well.
每个文件使用它自己单独的 localStorage 变量。如果我localstorage.clear()
在 project2 中,project1 和 project3 的设置也会丢失。
回答by Ben McCormick
localstorage is keyed to an origin. So if all of your projects are running on localhost, then you'll wipe all of your values when you use clear()
, and the only safe method is individual removal.
localstorage 以源为键。因此,如果您的所有项目都在 localhost 上运行,那么您将在使用 时擦除所有值clear()
,唯一安全的方法是单独删除。
In a production environment, each project should have its own domain and clear
should be safe.
在生产环境中,每个项目都应该有自己的域并且clear
应该是安全的。
So it's a question of knowing what else is on the current origin. If you control everything on the current origin and don't mind wiping it all, clear()
is the best choice and was designed for that purpose. If there are other parts of your code using localstorage or other projects hosted on the same origin then you will want to be more selective and use removeItem()
.
所以这是一个了解当前起源上还有什么的问题。如果您控制当前原点上的所有内容并且不介意将其全部擦除,clear()
则是最佳选择,并且是为此目的而设计的。如果您的代码的其他部分使用 localstorage 或托管在同一来源的其他项目,那么您将希望更具选择性并使用removeItem()
.
回答by markasoftware
clear()
clears everything on the current origin (https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript). Using clear()
on example.com will not affect the localStorage for example2.com. It is clearing data for all projects on your computer because all of the testing files that you have are on the same origin (http://localhost
or file:///C:\
). Therefore, it will be fine to use clear()
clear()
清除当前来源 ( https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript)上的所有内容。clear()
在example.com 上使用不会影响example2.com 的localStorage。它正在清除您计算机上所有项目的数据,因为您拥有的所有测试文件都来自同一来源(http://localhost
或file:///C:\
)。因此,使用它会很好clear()