zoukankan      html  css  js  c++  java
  • ES6_05_三点运算符和形参默认值

    三点运算符的用途:

    # 1. rest(可变)参数
    * 用来取代arguments 但比 arguments 灵活,只能是最后部分形参参数

    function fun(...values) {
    console.log(arguments);
    arguments.forEach(function (item, index) {
    console.log(item, index);
    });
    console.log(values);
    values.forEach(function (item, index) {
    console.log(item, index);
    })
    }
    fun(1,2,3);

    # 2. 扩展运算符
    let arr1 = [1,3,5];
    let arr2 = [2,...arr1,6];
    arr2.push(...arr1);

    &用例说明

    //1、 rest(可变)参数

    function foo(a, ...value){
    console.log(arguments);
    //arguments.callee(); //callee指向函数本身
    console.log(value);
    //arguments.forEach(function (item, index){
    //console.log(item, index);
    })
    
    value.forEach(function (item, index){
    console.log(item, index);
    })
    }
    foo(2, 65 ,33, 44);

    //2、扩展运算符

    let arr = [1, 6];
    let arr1 = [2 ,3 ,4 ,5];
    arr = [1,...arr1,6]
    console.log(arr);
    console.log(...arr1);
    
    * 形参的默认值----当不传入参数的时候默认使用形参里的默认值
    function Point(x = 1,y = 2) {
    this.x = x;
    this.y = y;
    }
    
    例:
    
    //定义一个点的坐标的构造函数
    function Point(x,y){
    this.x= x;
    this.y= y;
    }
    let point = new Point (23, 35);
    console.log(point);
    let point1 = new Point();
    console.log(point1);

    注意:以上用例代码在<script type="text/javascript"></script>中进行测试,包括其他知识点中的用例

    我是一个刚刚开始写博客的大可,内容有不详细或是错误的,还希望各位大佬私信我,我会进行纠正,谢谢啦!^-^
  • 相关阅读:
    HTTP Authorization
    php导出数组到csv格式demo
    nginx 配置ajax跨域访问php接口
    node.js和npm离线安装
    使用Docker快速搭建Nginx+PHP-FPM环境
    django中文件下载(HttpResponse)
    django 下载文件 无法正常打开
    C语言Review2_struct
    基础概念——回车换行
    C语言Review1_预处理器和宏
  • 原文地址:https://www.cnblogs.com/sunjiaojiao/p/11142608.html
Copyright © 2011-2022 走看看