未定义的变量,值== false与!值 [英] Undefined variables, value == false versus !value

查看:130
本文介绍了未定义的变量,值== false与!值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个用Javascript编写的非常简单的代码有问题,你能帮我吗?

I have a problem with a very simple piece of code written in Javascript, could you help me please?

这是我认为到目前为止我对javascript的理解和变量:

Here's what I think I have understand so far about javascript and variables:


  • 在布尔操作中将未定义的值计算为false

  • 使用在比较中==运算符,你会问两个值是否具有可比性,无论其类型如何

我在一个运动文件中找到了一个在线课程,我试着这样做,但是我没有得到同样的结果;主要问题是我通过if value == false {...}比较值,而解决方案是使用if!value {...}

I found an exercise file in a online course and I tried to do it, but I didn't got the same result expected in the lesson; the main problem was that I was comparing the value through a "if value == false { ... }" while the solution was using a "if !value { ... }"

所以我决定写一个非常短的代码,以便自己尝试,但我的结果好坏参半。在下面的示例中,我希望这个JS代码生成两个相同的警报(foo等于false),但是第一个if语句返回foo IS not equal false而第二个if返回(如预期的那样) foo等于false。

So I decided to write a very short code in order to try it by myself, but I'm getting mixed results. Here in the example below I would expect this JS code to generate two identical alerts ("foo is equal to false"), but instead the first if statement returns "foo IS NOT equal to false" while the second if returns (as expected) "foo is equal to false".

这就是我写的:

var foo = undefined;

if (foo == false) {
  alert("foo is equal to false");
} else {
  alert("foo is not equal to false"); // Javascript executes this row
}

if (!foo) {
  alert("foo is equal to false");  // Javascript executes this row
} else {
  alert("foo is not equal to false");
}

AFAIK这两个IF应该做同样的工作,当我尝试它时通过在第一行中替换值var foo = undefined;用var foo = 0;它按预期工作,0是另一个应该被评估为false的值,或者至少这是我记得的。

AFAIK the two IFs should do the same work, and infact when I tried it by replacing in the first line the value "var foo = undefined;" with "var foo = 0;" it worked as expected, and 0 is another value that should be evaluated to false, or at least this is what I remember.

你能告诉我我在做什么吗错误?

Could you tell me what I'm doing wrong?

推荐答案

== 算法抽象等式比较算法不是你可以简单地假设结果的东西,除非你知道算法。你需要知道它是如何工作的细节。

The == algorithm (Abstract Equality Comparison Algorithm) isn't something where you can simply assume an outcome unless you know the algorithm. You need to know the details of how it works.

例如, null undefined 是一个特例。除了被认为彼此相等之外,它们不进行任何类型转换。

For example, null and undefined are a special case. They do not do any type conversion other than to be considered equal to each other.

否则通常会进行类型转换,尝试将两个操作数都减少为通用类型。这通常最终是 toNumber 转换。

Otherwise there's typically a type conversion that tries to reduce both operands to a common type. This often ends up being a toNumber conversion.

这就是原因:


  • null == undefined; // true

null == 0; // false

+ null =='0'// true

因此,如果你知道算法是如何工作的,你知道 undefined 永远不等于 undefined null 以外的任何东西,但是其他类型不严格相等的类型可能会被强制到了相等的类型。

So if you know how the algorithm works, you know that undefined never equals anything except for undefined and null, but other types that are not strictly equal may be coerced down to types that are equal.

如果($ x) vs 那么做 (x == false)是完全不同的测试。

So doing if(!x) vs if(x==false) are entirely different tests.


  • if (!x)执行 toBoolean 转换。

if(x = = false)使用复杂算法来确定正确的转换。

if(x == false) uses a complex algorithm to decide the proper conversion.

所以用。 ..

if(x == false)

...如果 x 未定义,则决定不等于 false ,但如果 x 0 甚至0,它将被视为等于 false

...if x is undefined, it is determined to not be equal to false, yet if x is 0 or even "0", it will be considered equal to false.


  • 0 == false; // true

0== false; // true

这篇关于未定义的变量,值== false与!值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆