javascript 判断对象的值是否相等

 经常要判断两个对象的值是否相等 ,写了个方法用来比较,和对象深层copy的思路有点像,都是将对象进行类型判断 然后操作 。记录下。

 1 function compareobj(A,B){
 2                 if(A instanceof Array){
 3                     if(!(B instanceof Array)){
 4                         return false;
 5                     }
 6                     var aLen = A.length,
 7                         bLen = B.length;
 8                     if(aLen != bLen){
 9                         return false;
10                     }
11                     var isEqual = true,num = 0;
12                     for(var i = 0;i < aLen;i++){
13                         if(A[i]!=B[i] && typeof A[i]=='object' && typeof B[i]=='object'){
14                           isEqual = arguments.callee.apply(null,[A[i],B[i]]);
15                           //isEqual = compareobj(A[i],B[i]);
16                         }else{
17                           isEqual = A[i]===B[i];
18                         }
19                         if(isEqual){
20                             num++;
21                         }
22                     }
23                     if(num != aLen){
24                         return false;
25                     }else{
26                         return true;
27                     }
28                 }else if(A instanceof Date){
29                     if(!(B instanceof Date)){
30                         return false;
31                     }else{
32                         return A.getTime() == B.getTime();
33                     }
34                 }else if(A instanceof Object){
35                     if((B instanceof Array) || (B instanceof Date) || !(B instanceof Object)){
36                         return false;
37                     }else{
38                        var aLen=bLen=0;
39                        for(var i in A){
40                             aLen++;
41                        }
42                        for(var i in B){
43                             bLen++;
44                        }
45                        if(aLen != bLen){
46                             return false;
47                        }
48                        var isEqual = true,num = 0;
49                        for(var i in A){
50                          if(typeof A[i] == 'object' && typeof B[i] == 'object' && A[i]!=B[i]){
51                             isEqual = arguments.callee.apply(null,[A[i],B[i]]);
52                            // isEqual = compareobj(A[i],B[i]);
53                          }else{
54                             isEqual = A[i]===B[i];
55                          }
56                          if(isEqual){
57                             num++;
58                          }
59                        }
60                        if(num != aLen){
61                             return false;
62                        }else{
63                             return true;
64                        }
65                     }
66                 }else{
67                     if(B instanceof Object){
68                         return false;
69                     }else{
70                         return A===B;
71                     }
72                 }
73             }
posted @ 2012-08-02 14:20  lmh2072005  阅读(1697)  评论(0编辑  收藏  举报