0 == false =="" [英] 0 == false == ""

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

问题描述

问候,


现在我可以理解0等于假,但应该"也等于

假?


显然答案是肯定的。当我测试Firefox和IE时,他们

都说" == false。我假设这可以追溯到

JavaScript的原始规格。


不是很直观,但我想我可以编写代码。


- JsD

解决方案

Java脚本Dude写道:


现在我可以理解0等于假,但应该还等于

假?



是的,它应该。


显然答案是肯定的。当我测试Firefox和IE时,他们

都说" == false。



按设计工作。


我认为这可以追溯到JavaScript的原始规范。



也许是这样,但更重要的是JavaScript 1.1+和JScript 1.0+

实现了最终的ECMAScript语言规范

第3版修订版:


| 11.9.1等于运算符(==)

|

|制作EqualityExpression:EqualityExpression ==

| RelationalExpression评估如下:

|

| 1.评估EqualityExpression。

| 2.调用GetValue(结果(1))。

| 3.评估RelationalExpression。

| 4.调用GetValue(结果(3))。

| 5.执行比较结果(4)==结果(2)。 (第11.9.3节)

| 6.返回结果(5)。

|

| [...]

|

| 11.9.3抽象等式比较算法

|

|比较x == y,其中x和y是值,产生true或

|假。这样的比较如下:

|

| 1.如果类型(x)与类型(y)不同,请转到步骤14.


| 5.2算法约定

|

| [...]类型(x)用作?x的类型的简写。


| [...]

| 14.如果x为null且y未定义,则返回true。

| 15.如果x未定义且y为null,则返回true。

| 16.如果Type(x)是Number而Type(y)是String,

|返回比较结果x == ToNumber(y)。

| 17.如果Type(x)是String而Type(y)是Number,

|返回比较结果ToNumber(x)== y。

| 18.如果Type(x)是布尔值,则返回比较结果

| ToNumber(x)== y。

| 19.如果Type(y)是布尔值,则返回比较结果

| x == ToNumber(y)。


| 9.3 ToNumber

|

|运算符ToNumber将其参数转换为Number

|类型的值根据下表:

|

|输入类型结果

| --------------------------

|未定义的NaN

|空+0

| Boolean如果参数为true,则结果为1.

|如果参数为假,结果为+0。

| Number结果等于输入参数(无转换)。

|字符串请参阅下面的语法和注释。

|对象应用以下步骤:

| 1.调用ToPrimitive(输入参数,提示编号)。

| 2.致电ToNumber(结果(1))。

| 3.返回结果(2)。


因此(" == + 0):


| 1.如果Type(x)与Type(y)不同,请转到步骤14.

| [...]

| 14.如果x为null且y未定义,则返回true。

| 15.如果x未定义且y为null,则返回true。

| 16.如果Type(x)是Number而Type(y)是String,

|返回比较结果x == ToNumber(y)。

| 17.如果Type(x)是String而Type(y)是Number,

|返回比较结果ToNumber(x)== y。


| 9.3.1 ToNumber应用于字符串类型

|

| [...]

| StringNumericLiteral为空或仅包含空格是

|转换为+0。


因此(+0 == +0):


| 1.如果Type(x)与Type(y)不同,请转到步骤14.

| 2.如果Type(x)是Undefined,则返回true。

| 3.如果Type(x)为Null,则返回true。

| 4.如果Type(x)不是Number,请转到步骤11.

| 5.如果x是NaN,则返回false。

| 6.如果y是NaN,则返回false。

| 7.如果x与y的数值相同,则返回true。


| 5.2算法约定

|

|当算法要产生一个值时,指令

| ?返回x?用于表示算法的结果是

| x的值和算法应该终止。


不是很直观



是的,它是。例如,它允许


if(stringValue)


而不必与字符串的长度进行比较或检查

仅限空格的字符串值。


但我想我可以编写这个代码。



是的,你可以。使用严格等于运算符(`==='')代替

不执行隐式类型转换;它得到了很好的支持:

http://PointedEars.de/ es-matrix

PointedEars

-

var bugRiddenCrashPronePieceOfJunk =(

navigator.userAgent。 indexOf(''MSIE 5'')!= -1

&& navigator.userAgent.indexOf(''Mac'')!= -1

) // Plone,register_function.js:16


Thomas''PointedEars''Lahn在2008年1月15日11:13 PM发表以下内容:
< blockquote class =post_quotes>
Java脚本Dude写道:


>现在我可以理解0等于false,但应该"还等于
假?



是的,它应该。


>显然答案是肯定的。当我测试Firefox和IE时,他们都说'" == false。



按设计工作。


>我认为这可以追溯到JavaScript的原始规范。



也许是这样,但更重要的是JavaScript 1.1+和JScript 1.0+

实现了最终的ECMAScript语言规范

第3版的修订版:



不,比规范说的更重要的是什么

浏览器实际上是用代码做的。


-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq/index。 html

Javascript最佳实践 - http:// www.JavascriptToolbox.com/bestpractices/


Randy Webb< Hi ************ @ aol.comwrites:


Thomas''PointedEars''Lahn在2008年1月15日11:13 PM发表以下内容:

> Java脚本Dude写道:


>>现在我可以理解0等于false,但应该 ;"还等于
假?


是的,它应该。


>>显然答案是肯定的。当我测试Firefox和IE时,他们都说'" == false。


按设计工作。


>>我认为这可以追溯到JavaScript的原始规范。


也许是这样,但更重要的是JavaScript 1.1+和JScript 1.0+实现ECMAScript语言规范,该规范在其第3版的最终版本中说明版本:



不,比一些规范说的更重要的是

浏览器实际上用代码做什么。



而且,它的价值(仅适用于OP):


aundro @ paddy:〜

Greetings,

Now I can understand that 0 equal false, but should "" also equal
false?

Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false . I assume this goes back to the original spec for
JavaScript.

Not very intuitive but I guess I can code around this.

- JsD

解决方案

Java script Dude wrote:

Now I can understand that 0 equal false, but should "" also equal
false?

Yes, it should.

Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .

Works as designed.

I assume this goes back to the original spec for JavaScript.

Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:

| 11.9.1 The Equals Operator ( == )
|
| The production EqualityExpression : EqualityExpression ==
| RelationalExpression is evaluated as follows:
|
| 1. Evaluate EqualityExpression.
| 2. Call GetValue(Result(1)).
| 3. Evaluate RelationalExpression.
| 4. Call GetValue(Result(3)).
| 5. Perform the comparison Result(4) == Result(2). (Section 11.9.3.)
| 6. Return Result(5).
|
| [...]
|
| 11.9.3 The Abstract Equality Comparison Algorithm
|
| The comparison x == y, where x and y are values, produces true or
| false. Such a comparison is performed as follows:
|
| 1. If Type(x) is different from Type(y), go to step 14.

| 5.2 Algorithm Conventions
|
| [...] Type(x) is used as shorthand for ?the type of x?.

| [...]
| 14. If x is null and y is undefined, return true.
| 15. If x is undefined and y is null, return true.
| 16. If Type(x) is Number and Type(y) is String,
| return the result of the comparison x == ToNumber(y).
| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.
| 18. If Type(x) is Boolean, return the result of the comparison
| ToNumber(x) == y.
| 19. If Type(y) is Boolean, return the result of the comparison
| x == ToNumber(y).

| 9.3 ToNumber
|
| The operator ToNumber converts its argument to a value of type Number
| according to the following table:
|
| Input Type Result
| --------------------------
| Undefined NaN
| Null +0
| Boolean The result is 1 if the argument is true.
| The result is +0 if the argument is false.
| Number The result equals the input argument (no conversion).
| String See grammar and note below.
| Object Apply the following steps:
| 1. Call ToPrimitive(input argument, hint Number).
| 2. Call ToNumber(Result(1)).
| 3. Return Result(2).

Therefore ("" == +0):

| 1. If Type(x) is different from Type(y), go to step 14.
| [...]
| 14. If x is null and y is undefined, return true.
| 15. If x is undefined and y is null, return true.
| 16. If Type(x) is Number and Type(y) is String,
| return the result of the comparison x == ToNumber(y).
| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.

| 9.3.1 ToNumber Applied to the String Type
|
| [...]
| A StringNumericLiteral that is empty or contains only white space is
| converted to +0.

Therefore (+0 == +0):

| 1. If Type(x) is different from Type(y), go to step 14.
| 2. If Type(x) is Undefined, return true.
| 3. If Type(x) is Null, return true.
| 4. If Type(x) is not Number, go to step 11.
| 5. If x is NaN, return false.
| 6. If y is NaN, return false.
| 7. If x is the same number value as y, return true.

| 5.2 Algorithm Conventions
|
| When an algorithm is to produce a value as a result, the directive
| ?return x? is used to indicate that the result of the algorithm is
| the value of x and that the algorithm should terminate.

Not very intuitive

Yes, it is. For example, it allows for

if (stringValue)

without having to compare against the length of the string or to check for a
whitespace-only string value.

but I guess I can code around this.

Yes, you can. Use the Strict Equality Operator (`==='') instead, which does
not perform implicit type conversion; it is well-supported:

http://PointedEars.de/es-matrix
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16


Thomas ''PointedEars'' Lahn said the following on 1/15/2008 11:13 PM:

Java script Dude wrote:

>Now I can understand that 0 equal false, but should "" also equal
false?


Yes, it should.

>Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .


Works as designed.

>I assume this goes back to the original spec for JavaScript.


Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:

No, what is more important than what some Specification says is what
browsers actually do with code.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Randy Webb <Hi************@aol.comwrites:

Thomas ''PointedEars'' Lahn said the following on 1/15/2008 11:13 PM:

>Java script Dude wrote:

>>Now I can understand that 0 equal false, but should "" also equal
false?


Yes, it should.

>>Apparently the answer is yes. When I test both Firefox and IE, they
both say ""==false .


Works as designed.

>>I assume this goes back to the original spec for JavaScript.


Maybe so, but it is more important that JavaScript 1.1+ and JScript 1.0+
implement the ECMAScript Language Specification which says in the Final
revision of its 3rd Edition:


No, what is more important than what some Specification says is what
browsers actually do with code.

And, for what it''s worth (just for the OP):

aundro@paddy:~


这篇关于0 == false ==&quot;&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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