- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我理解下面的代码。
var day = 2;
switch (day) {
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday!!");
break;
case 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
它打印出“星期二!!”。
但是,为什么下面的方法不起作用?我虽然它应该打印相同的答案,但它一直打印“另一天”!?
var day = 2;
switch (day) {
case day == 1:
document.write("Monday");
break;
case day == 2:
document.write("Tuesday!!");
break;
case day == 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
最佳答案
switch 语句中的 case 尝试直接与 switch 条件匹配。所以你的片段:
var day = 2;
switch (day) {
case day == 1:
document.write("Monday");
break;
case day == 2:
document.write("Tuesday!!");
break;
case day == 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
实际上相当于:
var day = 2;
switch (day) {
case false:
document.write("Monday");
break;
case true:
document.write("Tuesday!!");
break;
case false:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
并且由于 day
不等于 true
或 false
(因为它实际上是 2
>),开关会转为默认情况。
您可以看到案例使用 the docs 中的严格相等(强调我的):
A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using strict comparison, ===) and transfers control to that clause, executing the associated statements.
关于javascript - JS : Why don't the switch statement cases `case 2` and `case day == 2` print out the same result?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47764486/
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我试图查看当天的车辆销量,并创建另外两个列来告诉我过去 10 天的销量和过去 20 天的销量。同一天和同一辆车可能有多个销售。我的目标是获取不同的车辆和日期并查看他们的销售数量。 N 天计数应与该行中
我需要每天 12 小时在 azure 上使用 VM,在 EOD 时关闭并在第二天早上再次启动。我习惯使用 WASABi block 进行自动缩放。 What strategy should I use
我只需要显示天/天的后缀,我该如何实现?它不起作用: java.lang.IllegalStateException: No field to apply suffix to.. privat
尝试使用 HTML 表单和 PHP 更新数据库字段时出现上述错误消息。不确定我的代码有什么问题(我知道它不安全,我只是将它用于个人用途)。我已经检查过,数据库 days 确实存在。 HTML:
谷歌搜索这个,但找不到答案。想知道这两个脚本是否有区别? +3 天: echo date( 'd.m.Y H:i:s', strtotime( '+3 day' ) ); +3 天: echo dat
我试图在某一天显示文本“A Day”,然后在第二天显示“B Day”。我可以使用偶数或奇数函数来执行此操作,然后配置 getDay 对象,但我尝试执行此操作,无论日、月或年如何。 例如,今天可能是“A
在 stack-overflow 中,有一个名为“爱好者”的徽章,通过“连续 30 天每天访问该网站”获得 如何在sql server中编写这个查询? 最佳答案 创建一个表 Id, LastVisit
$('') .attr('label', day) .attr('value', day) if(myDate.getDate(
如果我使用这个函数 pd.DatetimeIndex(dfTrain['datetime']).weekday 我得到了日期,但是我找不到任何给出日期名称的函数...所以我需要将 0 转换为星期一,将
I know I can use relativedelta to calculate difference between two dates in the calendar. However
我正在使用 DayPickerInput,我将其设置为 this (具有 2 天选择器输入的范围)。我想始终显示覆盖,我不想隐藏它。我知道 showOverlay Prop ,但它只在初始渲染期间显示
如何将类添加到 react-day-picker 的今天按钮? 从文档中似乎是可能的: http://react-day-picker.js.org/api/DayPicker#classNames
我需要将一个简单的“3.days”字符串转换为在屏幕上打印“3 days”的内容,该字符串将根据语言使用适当的语言环境来打印单词。 我想可能有一种我似乎找不到的在 Rails 上执行此操作的简单方法。
我想从 Github API GET,每天的贡献数。我正在制作一个网络应用程序,将 github 贡献的数量与我玩的 Dota 2 比赛的数量进行比较。 这张图应该能更清楚地说明事情。 http://
我想在一个月的第三天和最后一天运行一项作业。 下面的 cron 表达式正确吗? “0 0 3 3,L * * ?” 当我在 www.cronmaker.com 中尝试它时,它说这是一个无效的表达式。
我正在尝试为图像中的特定事件设置重复提醒。代码工作正常,但发生了一些正在改变创建事件的事情。请检查以下详细信息。 如果我尝试从循环中设置提醒,它会起作用,但如果重复出现,就会出现上述问题。 检查图像上
我知道新的一天(或小时/分钟,就此而言)没有事件监听器。但在我的 Chrome 扩展程序中,我需要知道新的一天何时开始,这意味着我必须使用 setInterval 函数来确定一天何时发生变化。但是,我
我有一个名为 stockins 的 table 和一个 column stock。因为我想在一周的第一天和一周的最后一天之间找到 max(stock)。 为此,我使用以下查询 select max(s
我们如何将 t.integer :missed 与 t.text :committed 集成,以便 当用户在 :level 中检查他 :missed 3 :committed 天时,他必须重新启动 :
我是一名优秀的程序员,十分优秀!