简体   繁体   English

JavaScript为什么getItem()有两种返回类型?

[英]JavaScript Why does getItem() have two return types?

I am currently following a basic JavaScript Course and there was an example, that confused me, because it seems that the getItem() function can have two return types. 我目前正在学习基本的JavaScript课程,并且有一个使我感到困惑的示例,因为getItem()函数似乎可以具有两种返回类型。

if(!localStorage.getItem('name')) {
  setUserName();
} else {
  var storedName = localStorage.getItem('name');
  myHeading.textContent =storedName;
}

So in the if condition getItem returns true or false depending on wether there is an Item stored in the localStorage or not. 因此,在if条件下,getItem返回true或false取决于是否在本地存储中存储了Item。 And later the return value is stored in a variable as a String to output it on the website(in the "else" part). 然后,返回值以字符串形式存储在变量中,以将其输出到网站上(在“其他”部分中)。 I googled for this and didn't find anything, but maybe I searched wrong, so I will be happy for any help I can get here. 我为此搜索了一下,却没有找到任何东西,但是也许我搜索错了,所以我很高兴能得到这里的任何帮助。 I don't know, if this code is enough to understand what I mean, but I will post more later on if necessary. 我不知道这段代码是否足以理解我的意思,但是如果需要的话,我会在稍后发布。

getItem returns: getItem返回:

A DOMString containing the value of the key. 包含键值的DOMString。 If the key does not exist, null is returned. 如果键不存在,则返回null。

And what is a DOMString ? 什么是DOMString

A DOMString is a UTF-16 String. DOMString是UTF-16字符串。 As JavaScript already uses such strings, DOMString is mapped directly to a String. 由于JavaScript已经使用了这样的字符串,因此DOMString被直接映射到字符串。

So, it returns a string . 因此,它返回一个string Strings (unless they are empty) are considered "truthy" in JS : 字符串(除非它们为空) 在JS中被认为是“真实的”

In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. 在JavaScript中,真实值是在布尔上下文中求值时认为是正确的值。 All values are truthy unless they are defined as falsy (ie, except for false, 0, "", null, undefined, and NaN). 除非将它们定义为虚假,否则所有值都是真实的(即,除了false,0,“”,null,undefined和NaN外)。

So, in other words, if it returns a non-empty string, then the if statement considers it true. 因此,换句话说,如果它返回一个非空字符串,则if语句认为它为true。 If not, it's considered false. 如果不是,则认为是错误的。

This also means the conclusion you drew, that getItem has "two return types" is incorrect. 这也意味着您得出的结论是getItem具有“两种返回类型”是不正确的。 It has a single return type, and that type can be evaluated for truthiness. 它只有一个返回类型,可以评估该类型的真实性。

Storage.getItem() Storage.getItem()

The getItem() method of the Storage interface, when passed a key name, will return that key's value. 存储接口的getItem()方法传递键名后,将返回该键的值。

Return value 返回值

A DOMString containing the value of the key. 包含键值的DOMString。 If the key does not exist, null is returned. 如果键不存在,则返回null。


if(!localStorage.getItem('name')) {
  setUserName();
} else {
  var storedName = localStorage.getItem('name');
  myHeading.textContent =storedName;
}

Your snippet will check if the local storage contains an item for the key 'name'. 您的代码段将检查本地存储区是否包含键“名称”的项目。 It will not return a boolean (true/false) but the object itself. 它不会返回布尔值(true / false),而是对象本身。 If there's no value for the key, it will return 'null'. 如果该键没有值,它将返回“ null”。

localStorage.getItem('name') returns: localStorage.getItem('name')返回:

  • Null : The script will execute the function setUserName() . :脚本将执行setUserName()函数。
  • Any value : The script will save the value under a variable called storedName . 任何值 :该脚本会将值保存在一个名为storedName的变量下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM