没毛病吧这个代码

来源:1-6 编程练习

daftweeknd

2020-12-17 14:43


<!DOCTYPE HTML>

<html>

<head>

    <meta charset="utf-8">

    <title>Untitled Document</title>

    

</head>

<body>

    <script type="text/javascript">   

        /*

         * param1 Array 

         * param2 Array

         * return true or false

         */

        function arraysSimilar(arr1, arr2){

            if(arr1.length!==arr2.length){

                return false;

            }

            var str=0,bool=0,num=0,func=0,date=0,win=0,undef=0,nul=0;

            for(const i of arr1){

                switch(typeof i){

                    case 'string':

                        str++;

                        break;

                    case 'boolean':

                        bool++;

                        break;

                    case 'number':

                        num++;

                        break;

                    case 'function':

                        func++;

                        break;

                    case 'date':

                        date++;

                        break;

                    case 'window':

                        win++;

                        break;

                    case 'undefine':

                        undef++;

                        break;

                    case 'null':

                        nul++;

                        break;

                }

            }

            for(const i of arr2){

                switch(typeof i){

                    case 'string':

                        str--;

                        break;

                    case 'boolean':

                        bool--;

                        break;

                    case 'number':

                        num--;

                        break;

                    case 'function':

                        func--;

                        break;

                    case 'date':

                        date--;

                        break;

                    case 'window':

                        win--;

                        break;

                    case 'undefine':

                        undef--;

                        break;

                    case 'null':

                        nul--;

                        break;

                }

            }

            if(str||bool||num||func||date||win||undef||nul){

                return false;

            }

            return true;

        }

        function f1(){


        }

        function f2(){


        }

        let arr1=[1,f2],arr2=[1,f1];

        if(arraysSimilar(arr1,arr2)){

            alert("判定结果:通过");

        }

        else{

            alert("判定结果:不通过");

        }

    </script>

    <script src="testData.js"></script>

</body>

</html>


写回答 关注

2回答

  • 慕莱坞600778
    2021-05-13 16:38:31

    哇,为啥你要写这么复杂

  • daftweeknd
    2020-12-17 15:17:39

    typeof对于window,undefine,null是无法区分的(都会判断为对象类型object),需要使用instanceof来进一步判断是哪种对象类型。

JavaScript深入浅出

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

281102 学习 · 1020 问题

查看课程

相似问题