问题描述
我在Chrome扩展程序中使用了 localStorage
。
I'm using localStorage
in a Chrome extension.
删除localStorage [key]
和 window.localStorage.removeItem(key)
或者它们的行为是否相同?
Is there any difference between delete localStorage[key]
and window.localStorage.removeItem(key)
or do they behave identically?
请注意,我选择 window.localStorage.removeItem(key)
来完全对应,但是 localStorage.removeItem(key)
看看在野外。
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.
推荐答案
在Chrome中没有区别,但我推荐使用localStorage.removeItem(key)习惯于这样做。这样,当您在网站中使用localStorage时,您已经在使用最有可能用于填充和填充过时浏览器的方法。
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.
这篇关于从localStorage中删除:我应该使用delete还是.removeItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!