使用`status == true'时,布尔条件始终为false. [英] Boolean condition is always false when using `status == true`
问题描述
所以我刚开始学习javascript,我现在在功能模块中,我在玩它,突然间我遇到了疑问:
So i just started learning javascript, I'm in the functions module now and I was playing around with it and suddenly i ran into a doubt:
这是为什么:
if(x==true){
return 1;
}
与此不同:
if(x){
return 1;
}
?
您知道,我有以下代码:
You see, i have this code:
function isAdult(age){
if(age >= 18){
return true;
}
return false;
}
function nameAndAge(string, boolean){
if(boolean == true){
var my_string = string + " is adult";
return my_string
}
var my_string = string + " is under age";
return my_string
}
var talisa_age = 22;
var talisa_name = "Talisa Maegyr";
var status = isAdult(talisa_age);
var str = nameAndAge(talisa_name,status);
console.log(str)
并且无论"talisa_age"值如何,我都会得到以下输出:
and regardless of the "talisa_age" value i get the following output:
"Talisa Maegyr is under age"
但是,如果我将nameAndAge的验证要求更改为
however, if i chaged the nameAndAge's validation to
if(boolean){
var my_string = string + " is adult";
return my_string
}
代码按预期工作...
the code works as intended...
推荐答案
如果您console.log(typeof status)
,您会看到它是一个字符串.为什么?答案是status
是一个特殊变量,
If you console.log(typeof status)
you'll see it's a string. Why? The answer is that status
is a special variable, window.status
, which no longer has any effect on the window status bar but is nonetheless converted back to a string (presumably for display) when assigned a value.
标准状态:
出于历史原因,
对象必须在获取时返回设置该字符串的最后一个字符串,并且在设置时必须将自身设置为新值.创建 Window
对象时,必须将属性设置为空字符串.它什么也没做.
For historical reasons, the
status
attribute on theWindow
object must, on getting, return the last string it was set to, and on setting, must set itself to the new value. When theWindow
object is created, the attribute must be set to the empty string. It does not do anything else.
因此,您的条件为if ("true" == true)
* ,即使强制也为假.将其更改为if ("true")
似乎可行,因为非空字符串是 truthy
So, your conditional is if ("true" == true)
*, which is false even with coercion. Changing it to if ("true")
appears to work because nonempty strings are truthy.
如果您为变量命名,则类似status_
之类的程序将正常运行.更好的是,避免在全局范围内声明变量,并且/或者使用const
或let
,它们比var
更可取,并且可以帮助防止此类奇怪的错误.
If you name your variable something else like status_
the program behaves normally. Even better, avoid variable declarations in the global scope and/or use const
or let
which are preferable to var
and can help prevent weird bugs like this.
问题的最小再现:
console.log(typeof status);
var status = 42;
console.log(typeof status);
一些可能的解决方法:
var status_ = 42;
console.log(typeof status_);
const status = 42;
console.log(typeof status);
(() => {
var status = 42;
console.log(typeof status);
})();
* :请注意,某些浏览器将window.status
视为只读,例如IE11.在这种情况下,status
将是""
,您将获得与众不同的异常行为.
*: Note that some browsers treat window.status
as read-only, for example IE11. status
would be ""
in such cases and you'll get differently unusual behavior.
进一步阅读:
- What is the scope of variables in JavaScript?
- Is there any reason to use the "var" keyword in ES6?
- Const in JavaScript: when to use it and is it necessary?
这篇关于使用`status == true'时,布尔条件始终为false.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!