zoukankan      html  css  js  c++  java
  • ES6--函数的参数

    参数展开(扩展)

    1、收集剩余的参数

    1 function show(a, b, ...args) {
    2     console.log(a);
    3     console.log(b);
    4     console.log(args);
    5 }
    6 
    7 show(1, 2, 3, 4, 5)

    打印结果如图。args为数组。

     位置必需在参数的最后一个(rest parameter 剩余参数)

    1 function show(a, b, ...args, c) {
    2     console.log(a);
    3     console.log(b);
    4     console.log(args);
    5 }
    6 //报错    Rest parameter must be last formal parameter

     2、展开数组

      展开后的效果就是直接将数组内容拿出来

    let arr1 = [1, 2, 3]
    let arr2 = [4, 5, 6]
    let arr = [...arr1, ...arr2]
    console.log(arr);   //[1, 2, 3, 4, 5, 6]

    默认参数

    1 function show(a, b = 1, c = 2) {
    2     console.log(a, b, c)
    3 }
    4 
    5 show(5) //5 1 2
    6 show(6, 9)  //6 9 2
  • 相关阅读:
    图像按钮
    提交按钮
    文件上传域
    Python创建虚拟环境
    Typecho使用技巧
    面向对象
    Python语法入门
    Python 基础数据类型
    与用户交互
    MySQL5.7安装教程
  • 原文地址:https://www.cnblogs.com/lianglanlan/p/9816995.html
Copyright © 2011-2022 走看看