zoukankan      html  css  js  c++  java
  • [js]js中函数传参判断

    1,通过||

            function fun(x,y){
                x=x||0;
                y=y||1;
                alert(x+y);
            }
            fun();
    

    2.通过undefined对比

           function fun(x,y){
                if(x==undefined){
                    x=100;
                }
                y=y==undefined?200:y;
                alert(x+y);
            }
            fun();
    

    3.通过argument

    		function fun(x,y){
    			x=arguments[0]?arguments[0]:100;
    			y=arguments[1]?arguments[1]:200;
    			return x+y;
    		}
    		alert(fun());
    		alert(fun(1,2));
    

    4,形参 实参 解释argument

        function fn(a,b)
        {
            console.log(fn.length); //得到是 函数的形参的个数
            //console.log(arguments);
            console.log(arguments.length); // 得到的是实参的个数
            if(fn.length == arguments.length)
            {
                console.log(a+b);
            }
            else
            {
                console.error("对不起,您的参数不匹配,正确的参数个数为:" + fn.length);
            }
            //console.log(a+b);
        }
        fn(1,2);
        fn(1,2,3);
    
  • 相关阅读:
    怎么在myeclipse中怎么集成Tomcat。
    JSP .基础概念
    继承
    封装
    什么是面向对象
    数据排序
    开发的套路
    Javabean规范
    转发和重定向
    md5加密
  • 原文地址:https://www.cnblogs.com/iiiiher/p/7161023.html
Copyright © 2011-2022 走看看