- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在学习基本的 JavaScript 类(class),有一个例子,这让我很困惑,因为 getItem() 函数似乎可以有两种返回类型。
if(!localStorage.getItem('name')) {
setUserName();
} else {
var storedName = localStorage.getItem('name');
myHeading.textContent =storedName;
}
因此,在 if 条件中,getItem 返回 true 或 false,具体取决于 localStorage 中是否存储了 Item。然后返回值作为字符串存储在变量中以将其输出到网站上(在“else”部分)。我用谷歌搜索了这个,没有找到任何东西,但也许我搜索错了,所以我很高兴能在这里得到任何帮助。我不知道这段代码是否足以理解我的意思,但如果有必要,我稍后会发布更多内容。
最佳答案
getItem
返回:
A DOMString containing the value of the key. If the key does not exist, null is returned.
什么是 DOMString
?
A DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String.
因此,它返回一个字符串
。字符串(除非它们为空)被视为 "truthy" in JS :
In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).
因此,换句话说,如果它返回一个非空字符串,则 if
语句认为它为 true。如果不是,则认为是假的。
这也意味着您得出的结论,即 getItem
有“两种返回类型”是不正确的。它具有单一返回类型,并且可以评估该类型的真实性。
关于JavaScript 为什么 getItem() 有两种返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48694376/
我是 Java 新手,所以如果这是一个愚蠢的问题,请原谅我。我得到了这门课: public class CustomDrawerAdapter extends ArrayAdapter { Conte
我知道还有其他几篇关于此主题的帖子,但我想粘贴我的代码,因为我相信其中可能存在导致我出现问题的错误。我的 FragmentStatePagerAdapter 返回了错误的位置,我不确定为什么。 这是我
因为我正在为我的应用程序使用自定义 listView 适配器,所以一些默认方法被覆盖了。当我调用适配器的 getItem(position) 方法时出现问题。由于此方法被重写,我试图找出所选项的结果返
我正在尝试为菜单设置事件项目 cookie。 所以我使用本地存储来完成此任务。当菜单项处于事件状态时,它会像这样存储在本地存储中 - $(function () {
我正在编写一个简单的优惠券应用程序,但最近我更新了一些依赖项和我的 Fragment不允许我 return null 错误照片: package com.szakes1.makdolannative.
我正在为我的单选按钮使用 ItemListener。我看到了很多 ItemListener 函数,但我的似乎工作方式不同。 ... jUserButton2.addItemListene
我试图从特定的 SWT 树中获取所有项目(并从它们创建一个 zk 树 - 并不重要)。问题是我只能获得树的第一个项目,而不能更深入。我使用 tree.getItems() 获取第一个项目,但是当我使用
我想覆盖在文件\administrator\components\com_content\models\article.php 中找到的方法 getItem() 第 257 行 public func
我为自定义 ListView 创建了一个自定义数组适配器,其中包含 TextView 和复选框。在我的自定义适配器中,我创建了一个名为 isChecked() 的方法 - static cla
我正在尝试在 View 页面中显示一些 fragment 。我使用从 NavigationDrawer 启动 PlanFragment FragmentTransaction transaction
我使用以下代码行将一些值存储到本地存储中; localStorage.setItem(event.lastEventId, JSON.stringify(data)); 查看浏览器控制台,存储如下所示
我目前正在学习基本的 JavaScript 类(class),有一个例子,这让我很困惑,因为 getItem() 函数似乎可以有两种返回类型。 if(!localStorage.getItem('na
设置:我想编写一个方法,该方法将采用嵌套数据对象和路径字符串,并尝试使用路径组件取消引用数据对象内的位置。 例如,您有一个类似 /alpha/bravo/0/charlie 的路径,该方法将返回 da
我试图从 ListView 中获取项目的值,但是当我尝试“adapter.getItem(position)”时收到转换错误。 根据日志,我假设 getItem 函数是发生错误的地方,但我不确定我还能
我有一个 MainActivity控制四个 fragment ,每个 fragment 都是一个选项卡。当我的主要 Activity 开始时,我在日志中打印了一行,以显示正在实例化的 fragment
我很难尝试让我的 ArrayAdapter 工作 - 之前使用自定义 ArrayAdapter 没有问题,但这次我不确定发生了什么。 我尝试使用适配器创建 ListView,它将显示每个 WeekVi
我在public View getView(int position, View convertView, ViewGroup parent) 中有一个带有以下(测试)代码的适配器: Curs
我的代码中有一个列表 ListView ,其中包含 2 个按钮和 2 个 TextView。我想通过单击按钮访问每个 textView 的文本。这是适配器的代码... public class Tra
这是我的代码 public static class TestPagerAdapter extends FragmentPagerAdapter { Context mContext;
这个问题已经有答案了: What do querySelectorAll and getElementsBy* methods return? (12 个回答) 已关闭 8 年前。 我的页面上有多个输
我是一名优秀的程序员,十分优秀!