- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
Moderator note: Please resist the urge to edit the code or remove this notice. The pattern of whitespace may be part of the question and therefore should not be tampered with unnecessarily. If you are in the "whitespace is insignificant" camp, you should be able to accept the code as is.
(a== 1 && a ==2 && a==3)
是否有可能在 JavaScript 中计算为 true
?
这是一家大型科技公司提出的面试问题。它发生在两周前,但我仍在努力寻找答案。我知道我们在日常工作中从未编写过这样的代码,但我很好奇。
最佳答案
如果您利用 how ==
works ,您可以简单地使用自定义 toString
(或 valueOf
)函数创建一个对象,该函数会在每次使用时更改返回的内容,从而满足所有三个条件。
const a = {
i: 1,
toString: function () {
return a.i++;
}
}
if(a == 1 && a == 2 && a == 3) {
console.log('Hello World!');
}
之所以可行,是因为使用了松散相等运算符。使用松散相等时,如果其中一个操作数的类型与另一个不同,引擎将尝试将一个操作数转换为另一个。对于左侧的对象和右侧的数字,它将尝试通过首先调用 valueOf
将对象转换为数字,如果它是可调用的,如果失败,它将调用 toString
。在这种情况下,我使用 toString
只是因为想到的是,valueOf
会更有意义。如果我改为从 toString
返回一个字符串,然后引擎会尝试将该字符串转换为一个数字,从而为我们提供相同的最终结果,但路径稍长。
关于javascript - (a== 1 && a ==2 && a==3) 可以评估为真吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48270127/
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Why can't Python handle true/false values as I expect?
我是不是遗漏了什么或者这是 ruby 中的错误? a = %w(foo bar baz) a.include? "foo" # => true a.size == 3
从 Modelica 文档来看,注释 Evaluate 似乎只对参数有影响: https://build.openmodelica.org/Documentation/ModelicaReferenc
为了避免嵌套的 if 语句并提高可读性,我想创建一个switch(true){ ... } Coldfusion 中的声明。我在 php 中经常使用这个,但是当我在 Coldfusion 中尝试这个时
嗨,我正在尝试处理 ajax json 响应 这是我的代码 success: function (j) { switch(true) { case (j.cho
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
这个问题在这里已经有了答案: How can I return pivot table output in MySQL? (10 个答案) 关闭 5 年前。 我正在尝试构建一个以唯一列值作为列名的表
我制作了一个简单的 JDialog,其中包含一个标签和一个按钮,它基本上相当于信息对话框。所以在对话框中,有一个方法 display() 我在其中调用了 setVisible(true) 五次。 据我
在 bash 4.2.8(1)-release (x86_64-pc-linux-gnu) 在 Ubuntu 11.04 上这个命令 [ $(wc -l /var/www/some.log|cut -
我正在使用 c 语言进行并发处理,我有一个进程池。为此,我让每个 child 都在一个 While (True) 循环中。为了杀死 child ,我正在使用一个全局变量和一个信号处理程序来修改它来打破
我正在尝试选择填写了字段的数据库条目。数据库有两种插入数据的方式,一种输入评论,一种不输入,我希望只选择填写了评论的行。 $requete = "SELECT * FROM daysoff WHER
如何在 JavaMail session 中setDebug(true) 捕获流并在我的日志记录框架中使用它? (缺少下载源代码,更改接受流作为参数的方法,重新编译它,...) 更一般地说,Java
我是 JavaScript 的新手,我刚刚发现了我无法理解的奇怪行为: var magicVar = Math.sin; magicVar == true; // it returns false m
对此感到困惑。 在两台服务器上运行相同版本的 MySQL。 (从完全相同的 rpm 构建)- 沿线的某个地方,一些开发人员改变了一些东西...... 服务器 1: mysql> select ( no
我在查看 OpenSSL 中使用的一些预处理器宏时,从 crypto/stack/safestack.h 中发现了以下内容: #define CHECKED_STACK_OF(type, p) \
所以我遇到了一个问题,我的正则表达式看起来像这样:/true|false/。 当我检查单词 falsee 时,我从这个正则表达式中得到一个 true,有没有办法将它限制为确切的 true 或 fals
我正在对这个恶意 JavaScript 行进行一些试验:var undefined = true; JavaScript 中每个未初始化的变量都有 undefined 的值,这只是一个保存特殊值 'u
我想将 PHP 的微时间存储为我在 MySQL 中的时间戳。 我去过told最好用 DECIMAL 存储它,但我找不到理想的大小。 有谁知道 microtime(true) 返回的最大大小是多少,所以
在 PHP 中,当您在 URL 中包含诸如“var=true”之类的内容时,URL 中的“true”和“false”是否被转换为 boolean 变量,或者它们是否等于文本“true”还是“假”?例如
我是一名优秀的程序员,十分优秀!