javascript 如何将本地存储项设置回 null。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15193461/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:49:33  来源:igfitidea点击:

How to set localstorage item back to null.

javascripthtmlnulllocal-storage

提问by David

I'm using a system to alert users when a major update has happened to a site, and I do it with LocalStorage, when I made the system, I made the system check if tip was "null", then set "tip" to true when they got the alert. Now, I would like to set the 'tip' localstorage back to null, and use "tip2" instead.

我正在使用一个系统在站点发生重大更新时提醒用户,并且我使用 LocalStorage 来做到这一点,当我制作系统时,我让系统检查提示是否为“空”,然后将“提示”设置为当他们收到警报时为真。现在,我想将 'tip' localstorage 设置回 null,并改用“tip2”。

Would I do this? localStorage.setItem('tip', 'null');

我会这样做吗?localStorage.setItem('tip', 'null');

回答by Michael Theriot

localStorage.removeItem('tip')if you are aiming to remove the key

localStorage.removeItem('tip')如果您的目标是删除密钥

localStorage.setItem('tip', 'null')if you just want to set it to the string "null"

localStorage.setItem('tip', 'null')如果您只想将其设置为字符串“null”

回答by whitneyland

There is no way to set a localStorage item to have a value of null. Use remove instead.

无法将 localStorage 项设置为 null 值。改用删除。

Any localStorage item either has a string value, or it does not exist. No other type is possible, therefore no variation of null is possible either.

任何 localStorage 项要么具有字符串值,要么不存在。没有其他类型是可能的,因此 null 的变化也不可能。

The confusing part is that as you noticed in your example, if you call localStorage.getItem('tip') and it does not exist, you'll see a null return value in the debugger or when attempting to display the value.

令人困惑的部分是,正如您在示例中所注意到的,如果您调用 localStorage.getItem('tip') 并且它不存在,您将在调试器中或尝试显示该值时看到一个 null 返回值。

As unintuitive as this is, it doesn't mean a local storage item actually exists with a value of null. It really means the item 'tip' does not exist at all. There is not even a single internal trace anywhere of anything related to the key ‘tip'.

尽管这很不直观,但这并不意味着本地存储项实际存在且值为 null。这真的意味着项目“提示”根本不存在。甚至没有任何与关键“提示”相关的任何内部痕迹。

For an item that does exist, if you are trying to set the item "back to null" (which is impossible), what you should be doing instead is to removethe item.

对于确实存在的项目,如果您尝试将该项目设置为“返回空”(这是不可能的),那么您应该做的是删除该项目。

In your example, this will cause you see the return value displayed as null again, like when you first called localStorage.getItem('tip'), even though it's not null, it just doesn't exist at all. To do that you can remove the item as described below.

在您的示例中,这将导致您再次看到返回值显示为 null,就像您第一次调用 localStorage.getItem('tip') 时一样,即使它不是 null,它也根本不存在。为此,您可以如下所述删除该项目。

Just remember all you ever need is get, set, and remove.

请记住,您所需要的只是getsetremove

localStorage.getItem('tip')                            // Get item value
localStorage.setItem('tip','cool stuff')    // Set item value
localStorage.removeItem('tip')                      // Remove item
if (localStorage.getItem('tip') === null)  // Check if item exists

localStorage.getItem('tip')                            // 获取项目值
localStorage.setItem('tip','cool stuff')    // 设置项目值
localStorage.removeItem('tip')                      // 删除项目
if (localStorage.getItem('tip') === null)  // 检查项目是否存在

Technically, @Michael Theriot‘s answer was suggesting to set the item tip to a string value of "null". That's not much different that setting it to “dog”, in that neither one of them is actually a system recognized null value.

从技术上讲,@Michael Theriot 的回答是建议将项目提示设置为字符串值“null”。这与将其设置为“dog”并没有太大不同,因为它们中的任何一个实际上都不是系统识别的空值。

I'm guessing that's not what you meant, however even if it were, I would tend to advise against the practice because it can become confusing to others maintaining your code.

我猜这不是你的意思,但即使是这样,我也倾向于反对这种做法,因为它可能会让其他维护你的代码的人感到困惑。