zoukankan      html  css  js  c++  java
  • JS函数参数

    1、实参数大于形参数:前几个参数相对应赋值,多余的忽略

    function say(name,message){
        console.log('Hello' + name + message);
    }
    
    say('World!','ByeBye!','World’);
    //控制台打印出:HelloWorld!ByeBye!

    2、实参数小于形参数:前几个参数对应赋值,不足的 undefined

    function say(name,message){
        console.log('Hello' + name + message);
    }
    
    say('World!’);
    //控制台打印出:HelloWorld!undefined

    注意:在JS中变量定义的时候,如果未给一个变量赋初值那么该变量的类型为 undefiend

    3.通过arguments 来实现函数参数的“调用”:

    function say(name, message){
          console.log('Hello' + arguments[0] + arguments[1]);
          console.log(arguments.length);
    }
    say('World!', 'ByeBye!’);

    也可以按下面写法

    function say(){
        console.log('Hello' + arguments[0] + arguments[1]);
    
        console.log(arguments.length);
    }
    
    say('World!', 'ByeBye!’);

    控制台打印效果都是一样的;即:HelloWorld!ByeBye!

    即:在这里你可以这样理解;当发生函数调用的时候,实参被保存在叫做arguments的“数组”当中;而arguments中对应下标的值在发生函数调用的时候始终与被调用函数的参数保持;

    内存分析:

    function say(name, message){
          console.log(arguments[1] == message);
          arguments[1] = 'World!';
          console.log(arguments[1] == message);
    }
    
    say('World!', 'ByeBye!’);//控制台打印:true;true;

    不过并不是说读取这两个值会访问相同的内存空间,他们的内存空间是独立的,arguments中的值不会发生改变,也就是参数的改变不会改变arguments中对应的值

    function say(name, message){
          console.log(arguments.length);
    }
    
    say('World!');//控制打印:1;

    arguments对象的长度是由传入的参数个数决定,不由定义函数的参数的个数决定。

    美好生活
  • 相关阅读:
    Java 数组
    【转】Centos 设置IP地址的几种方式
    【转】CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”
    【转】CentOS图形界面的开启与关闭
    【转】linux Centos 6.5 安装桌面环境GNOME
    VirtualBox 更改主机和虚拟机之间的鼠标切换热键
    【转】Virtualbox虚拟机配置安装CentOS 6.5图文教程
    0622 python 基础05
    0617 python 基础04
    0610 python 基础03
  • 原文地址:https://www.cnblogs.com/ssbydk/p/10180051.html
Copyright © 2011-2022 走看看