1 function isEqual(a, b) {
 2     //如果a和b本来就全等
 3     if (a === b) {
 4         //判断是否为0和-0
 5         return a !== 0 || 1 / a === 1 / b;
 6     }
 7     //判断是否为null和undefined
 8     if (a == null || b == null) {
 9         return a === b;
10     }
11     //接下来判断a和b的数据类型
12     var classNameA=toString.call(a),
13         classNameB=toString.call(b);
14     //var classNameA = typeof(a), (兼容IE的写法)
15         //classNameB = typeof(b);
16     //如果数据类型不相等,则返回false
17     if (classNameA !== classNameB) {
18         return false;
19     }
20     if (a instanceof Date)
21     {
22         classNameA = '[object Date]';
23     }
24 
25     //如果数据类型相等,再根据不同数据类型分别判断
26     switch (classNameA) {
27         case '[object RegExp]':
28         case '[object String]':
29         case 'string':
30             //进行字符串转换比较
31             return '' + a === '' + b;
32         case 'number':
33         case '[object Number]':
34             //进行数字转换比较,判断是否为NaN
35             if (+a !== +a) {
36                 return +b !== +b;
37             }
38             //判断是否为0或-0
39             return +a === 0 ? 1 / +a === 1 / b : +a === +b;
40         case '[object Date]':
41         case '[object Boolean]':
42         case 'boolean':
43             return +a === +b;
44     }
45     //如果是对象类型
46     if (classNameA == '[object Object]' || classNameA=='object') {
47         //获取a和b的属性长度
48         var propsA = Object.getOwnPropertyNames(a),
49             propsB = Object.getOwnPropertyNames(b);
50         if (propsA.length != propsB.length) {
51             return false;
52         }
53         for (var i = 0; i < propsA.length; i++) {
54             var propName = propsA[i];
55             //如果对应属性对应值不相等,则返回false
56             if (a[propName] !== b[propName]) {
57                 return false;
58             }
59         }
60         return true;
61     }
62     //如果是数组类型
63     if (classNameA == '[object Array]') {
64         if (a.toString() == b.toString()) {
65             return true;
66         }
67         return false;
68     }
69 }