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对象的长度是由传入的参数个数决定,不由定义函数的参数的个数决定。

    美好生活
  • 相关阅读:
    【Vue】状态管理
    【Vue】路由
    【Vue】组件
    【Vue】基础(数据 & 计算属性 & 方法)
    【Vue】基础(虚拟DOM & 响应式原理)
    【Vue】基础(生命周期 & 常用指令)
    【Vue】搭建开发环境
    【Mongodb】事务
    【Mongodb】视图 && 索引
    【Mongodb】聚合查询 && 固定集合
  • 原文地址:https://www.cnblogs.com/ssbydk/p/10180051.html
Copyright © 2011-2022 走看看