JS函数02

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
//        test();
        function test(){
            alert('this is a test');
        }
        //test();
        //函数名称严格区分大小写
        function TEST(){
            alert("hello king");
        }
//        TEST();
        //函数名称重复会产生覆盖
        //test();
        function test(){
            alert('hello nana');
        }
//        test();
        function test1(){
            alert('this is test1 function');
        }
        //alert(test1());
        function test2(){
//            return null;
//            return undefined;
//            return;
//            return true;
//            return 'this is king';
//            return 1.2;
            alert('this is a test');
//            return 1;
        }
        //alert(typeof (alert('this is a test')));
        
        
        function calc(num1,num2){
            return num1+num2;
        }
//        alert(calc(1,2));
//        alert(window.calc(3,5));
//        alert(calc(1,2,3,4,5,6));
        function calc1(num1=1,num2=2){
            return num1+num2;
        }
        function calc1(num1,num2){
            num1=num1||1;
            num2=num2||2;
            return num1+num2;
        }
        alert(calc1());
        </script>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    <script type="text/javascript">
    //实现默认参数的形式
    function calc4(x,y){
        x=x||0;
        y=y||0;
        return x+y;
    }
    function calc4(x,y){
        if(x===undefined){
            x=0;
        }
        y=y===undefined?0:y;
        return x+y;
    }
    //alert(calc4());
    //alert(calc(1,3,4));                //控制着对象的长度
    function calc(x,y){
        //return arguments;
//        alert(arguments[0]);
//        alert(arguments.length);
        x=arguments[0]?arguments[0]:0;
        y=arguments[1]?arguments[1]:0;
        return x+y;
    }
    //alert(calc());
    //alert(calc(1,2));
//可变参数形式的函数
    function test(){
        var paramsNum=arguments.length;//得到传入参数的个数
        var sum=0;
        for(var i=0;i<paramsNum;++i){
            sum+=arguments[i];
        }
        return sum;
    }
    //alert(test(1,2,3,4,5,6));
    function test1(){
        var paramsNum=arguments.length;
        var max=0;
        for(var i=0;i<=paramsNum-1;i++){
            if(arguments[i]>max){
                max=arguments[i];
            }
        }
        return max;
    }
    alert(test1(123,3432,23456,445643));
    </script>
    </body>
</html>

 

posted @ 2018-08-27 11:07  冯志国  阅读(113)  评论(0编辑  收藏  举报