JS字符串比较的一个有趣的讨论

哈哈,在MSN中对JS字符串比较的一个有趣的讨论~

 

哈哈,在MSN中对JS字符串比较的一个有趣的讨论~

JSW 发送 2008-4-28 14:58:
在不,有个javascript的问题不明白,可以问你不?
var str1=new String("");
var str2=new String("");
alert(str1==str2);
这两个结果为什么不一样呢

JSW 发送 2008-4-28 14:58:
我总觉得该是一样的

Aimingoo 说:
好象我在书里是讲过这个的吧。

JSW 说:
嗯,有的,但是没怎么明白==,和===的区别,我的理解,上面这样应该true的,但是还是false

Aimingoo 说:
是在P45页(就是《JavaScript语言精髓与编程实践》啦)。

Aimingoo 说:
我的意思是说,引用类型作比较的时候,并不会逐字符的进行check,而是检查它们的引用地址。也就是说,只要不是“同一个对象”的不同引用,就不会等值,也不会全等。

Aimingoo 说:
显然,你的代码中用new String()两次,生成了两个引用。所以……

JSW 说:
有没有一个==和===执行出来结果不一样的情况呢?我发觉好像都是一致的

Aimingoo 说:
你把其用任意一个转换成值类型就可以了。

Aimingoo 说:
例如:
alert(str1.valueOf() == str2);

Aimingoo 说:
或者:
alert(str1+'' == str2)

JSW 说:
呵呵,明白了,谢谢哈
不好意思打扰您工作了

Aimingoo 说:
我把这个贴到51js里去吧。挺有趣的一个问题。
=======================================================================
alert(null==undefined)
alert(null===undefined)
=======================================================================
同理
<script type="text/javascript">
var arr1=[1,2,3,4];
var arr2=[1,2,3,4];
alert(arr1==arr2);
alert(arr1.toString()==arr2);
</script>
=======================================================================
哈哈,楼上的意思是说如何比较两个数组是否相等。当然,这是个好点子。不过更完整的做法是这样:
(), var arr1=[2,1,4,3];
var arr2=[1,3,2,4];
alert( arr1.length==arr2.length && (arr1.sortarr2.sort(), arr1.toString() == arr2) );
=======================================================================
字串是按内容,对象按地址吧
=======================================================================
这个问题以前我跟你讨论过的呀 也是在MSN里面 还争论了string是否是引用类型的

这个是标准的描述

The Strict 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), return false.
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.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11.If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same
length and same characters in corresponding positions); otherwise, return false.
12. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
13.Return true if x and y refer to the same object or if they refer to objects joined to each other (see
13.1.2). Otherwise, return false.

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.
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.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11.If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same
length and same characters in corresponding positions). Otherwise, return false.
12. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
13.Return true if x and y refer to the same object or if they refer to objects joined to each other (see
13.1.2). Otherwise, return false.
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).
20.If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).
21.If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
22. Return false.
=======================================================================

QUOTE:
原帖由 [i]Aiming[/i] 于 2008-4-29 10:22 发表
哈哈,楼上的意思是说如何比较两个数组是否相等。当然,这是个好点子。不过更完整的做法是这样:


var arr1=[2,1,4,3];
var arr2=[1,3,2,4];
alert( arr1.length==arr2.length && (arr1.sort(), arr2.sor ...

这个如果想把对象也当作纯数据类型的话 还可以比较toJSON (当然还需要实现一下了)

Aiming的这个应该是把数组看作集合了 如果把数组看作序列的话 还是应该用前面的比较方式吧
========================================================================
嗯,赞同winter的看法
应该使用toJsonString转化一下再比较才能通用,否则如果Array里面含有对象,就不对了。
<script>
var arr1=[2,1,{z:2},3];
var arr2=[2,3,1,{z:1}];
alert( arr1.length==arr2.length && (arr1.sort(), arr2.sort(), arr1.toString() == arr2) );
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值