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>中进行测试,包括其他知识点中的用例

    我是一个刚刚开始写博客的大可,内容有不详细或是错误的,还希望各位大佬私信我,我会进行纠正,谢谢啦!^-^
  • 相关阅读:
    百度地图API示例之小实践 添加代理商标注
    MySQL分组操作
    MySQL连表操作
    MySQL多对多操作
    MySQL一对一操作
    MySQL唯一索引
    MySQL用户授权管理
    MySQL外键操作
    MySQL删操作
    MySQL增操作
  • 原文地址:https://www.cnblogs.com/sunjiaojiao/p/11142608.html
Copyright © 2011-2022 走看看