我这个那里有问题,求指教

来源:1-6 编程练习

慕函数2531606

2020-03-29 13:29

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb18030">
    <title>Untitled Document</title>
   
</head>
<body>
    <script type="text/javascript">  
        /*
         * param1 Array
         * param2 Array
         * return true or false
         */
        function arraysSimilar(arr1, arr2){
            if(arr1 instanceof Array)||!(arr2 instanceof Array){
                return false;
            }
            if(arr1.length!==arr2.length){
                return false;
            }
            var i=0;
            n=arr1.length;
            countMap1={};
            countMap2={};
            var t1,t2;
            TYPES=['string','boolean','number','undefined','null','function','date','window'];
            for(;i<n;i++){
                t1=typeOf(arr1[i]);
                t2=typeOf(arr2[i]);
                if(countMap1[t1]){
                    countMap1[t1]++;
                }else{
                    countMap1[t1]=1;
                }
                if(countMap2[t2]){
                    countMap2[t2]++;
                }else{
                    countMap2[t2]=1;
                }
            }
            function typeOf(ele){
                var r;
                if(ele===null)r='null';
                else if(ele instanceof Array) r='array';
                else if(ele ===window) r='window';
                else if(ele instanceof Date) r='date';
                else r=typeof ele;
                return r;
            }
            for(i=0,n=TYPES.length;i<n;i++){
                if(countMap1[TYPES[i]]!==countMap2[TYPES[i]]){
                    return false;
                }
            }
            return true;
        }
    </script>
    <script src="testData.js"></script>
</body>
</html>

写回答 关注

1回答

  • WE_Xing
    2020-03-30 16:41:30
    if(!(arr1 instanceof Array) || !(arr2 instanceof Array))

    少个 ' ! ' 还是?

JavaScript深入浅出

由浅入深学习JS语言特性,且解析JS常见误区,从入门到掌握

281100 学习 · 1020 问题

查看课程

相似问题