zoukankan      html  css  js  c++  java
  • JS参数传递

    JS中函数的形参没有数据类型,函数调用的时候并不对传入的实参做任何的检查。例如:

    $(function() {
    
        test1("Hello", 100);
    });
    
    function test1(arg1, arg2) {
        console.log(arg1);
        console.log(arg2);
    }

    输出结果:

    其实JS甚至不检查传入参数的个数,例如:

    $(function() {
        console.log("+--TOP--+");
    
        //JS不检查传入参数的个数
        var a = argumentTest1(1, 2, 30, 14, 5);
    
        for (var i = 0;i<a.length;i++) {
            console.log(a[i]);
        }
    
    });
    
    function argumentTest1() {
        //arguments是传入参数的集合
        return arguments;
    }

    输出结果:

    对于某些参数,如果想将其设为可选的,可在不传实参,函数调用时不传实参的话,形参的值默认为undefined。如果是形参在中间,可显式的传入null或者是undefined实现可选参数。对于可选参数的函数内,应该对参数作检测。例如:

    function argumentTest2(arg1, arg2, arg3) {
        if (arg1) {
            console.log("arg1:" + arg1);
        }
        if (arg2) {
            console.log("arg2:" + arg2);
        }
        if (arg3) {
            console.log("arg3:" + arg3);
        }
    }
    
    $(function() {
        console.log("+--01--+");
    
        argumentTest2(1, 2);
    
        console.log("+--02--+");
    
        argumentTest2(1, null, 2);
    
    });

    结果:

  • 相关阅读:
    Docker 国内镜像源
    SeMF安装指南
    CGI environment variables
    OpenResty + ngx_lua_waf使用
    OpenResty源码编译安装
    Ubuntu安装DVWA
    C安全编码实践
    [译]The Complete Application Security Checklist
    HTTP 安全头配置
    AWVS 10.5使用指南
  • 原文地址:https://www.cnblogs.com/efanfan/p/2876613.html
Copyright © 2011-2022 走看看