从 localStorage 中删除:我应该使用 delete 还是 .removeItem?
我在 Chrome 扩展程序中使用 localStorage
。
delete localStorage[key]
和 window.localStorage.removeItem(key)
之间有什么区别吗?或者它们的行为是否相同?
请注意,我选择 window.localStorage.removeItem(key)
与 spec,但是 localStorage.removeItem(key)
可能是它在野外的样子。
I'm using localStorage
in a Chrome extension.
Is there any difference between delete localStorage[key]
and window.localStorage.removeItem(key)
or do they behave identically?
Note that I choose window.localStorage.removeItem(key)
to correspond exactly to the spec, but localStorage.removeItem(key)
is how it would probably look in the wild.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Chrome 中没有区别,但我建议使用 localStorage.removeItem(key) ,以便您养成这样做的习惯。这样,当您在网站中使用 localStorage 时,您已经在使用最有可能与过时浏览器的 shim 和 polyfill 配合使用的方法。
There's no difference in Chrome, but i recommend using localStorage.removeItem(key) so that you get in the habit of doing so. That way, when you go to use localStorage in a website, you are already using the method that is most likely to work with shims and polyfills for outdated browsers.