Javascript 0 =='0'。解释这个例子 [英] Javascript 0 == '0'. Explain this example

查看:335
本文介绍了Javascript 0 =='0'。解释这个例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个帖子中找到了thoose示例,但我没有得到它。

I found thoose examples in another thread but I dont get it.

0 == '0'            // true

左边的0我转换为false(唯一的数字)。右边是一个非空字符串,它转换为true。
那么怎么能
假==真 - >真

0 to the left i converted to false(the only number that does that). The right is a non empty string wich converts to true. So how can false == true --> true

我错过了什么?

编辑:很多抱怨。我不明白我的具体情况。 (并且顶部的链接正好是我复制并粘贴此案例的地方)

a lot of complaining. I didn't understand my specific case. (And the link on the top is exactly where I have copy and pasted this case from)

推荐答案

这是一个正式答案你的问题(引用的部分,底部的链接)和分析:

Here is an official answer to your question (the quoted parts, link at the bottom) and analysis:


如果操作数不是同一类型,则等于运算符会转换操作数,然后应用严格比较。如果操作数是数字或布尔值,操作数将尽可能转换为数字;否则,如果任一操作数是字符串,则尽可能将字符串操作数转换为数字。如果两个操作数都是对象,则JavaScript比较内部引用,当操作数引用内存中的同一对象时,这些内部引用相等。
语法

The equality operator converts the operands if they are not of the same type, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory. Syntax

x == y

示例

3 == 3 // true

3 == 3 // true

3== 3 // true

"3" == 3 // true

3 =='3'// true

3 == '3' // true

这意味着,正如我读到的那样,第一个3(整数)被转换字符串以满足比较,因此它变为'3'=='3',这是真的,与你的情况相同。

This means, as I read it, that the first 3 (integer) is converted to string to satisfy the comparison, so it becomes '3' == '3', which is true, same as in your case with zeroes.

注意:我认为不同JS引擎中的转换可能会有所不同,尽管它们必须在ECMAScript特定的情况下统一 - http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 引用@Derek朕会功夫)。这个假设是基于主观和命令性的观点,并非所有浏览器和JavaScript引擎都符合ECMAScript。

NOTE: I assume that the converion may vary in different JS engines, although they have to be unified under ECMAScript specifiction - http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 (quoted @Derek朕會功夫). This assumption is made on a subjective and imperative opinion that not all browsers and JavaScript engines out there are ECMAScript compliant.


如果操作数严格相等(见上文)且没有类型转换,则identity运算符返回true。

The identity operator returns true if the operands are strictly equal (see above) with no type conversion.

在答案结尾处的同一资源上找到的身份/严格相等(===)将跳过自动类型转换(如上所述)并且还将执行类型检查,以确保我们具有完全匹配,即上面的表达式将失败,例如:

The Identity / strict equality (===) found on the same resource at the end of the answer will skip the automatic type conversion (as written above) and will perform type checking as well, to ensure that we have exact match, i.e. the expression above will fail on something like:

typeof( int)== typeof(string)

这是大多数语言中常见的运算符,类型较弱:

This is common operator in most languages with weak typing:

http://en.wikipedia.org/wiki/Strong_and_weak_typing

我会这么说一个人应该确定一个函数/方法将返回什么,如果这样的函数/方法即将返回数字(整数/浮点数)它应该坚持到最后,否则相反的做法可能会由于许多原因而削减你的头还有一个问题就在这个问题上。

I would say that one should be certain what a function/method will return, if such function/method is about to return numbers (integers/floating point numbers) it should stick to that to the very end, otherwise opposite practices may cut your head off by many reasons and one lays in this question as well.

上述内容对于其他类型弱的语言也有效,例如PHP。

The above is valid for other languages with weak typing, too, like PHP for example.

阅读更多内容,请参阅第二位(平等运营商):

Read more of this, refer second head (Equality operators):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

这篇关于Javascript 0 =='0'。解释这个例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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