美文网首页
ES6中的箭头函数以及扩展

ES6中的箭头函数以及扩展

作者: 一只码农的成长 | 来源:发表于2018-06-27 22:00 被阅读0次

    1、回顾

    用ES5定义一个加法函数:

    function add(a,b){
        return a+b;
    }
    console.log(add(1,2));    //3
    

    如果b有默认值:(这个时候可以只传一个参数)

    function add(a,b=1){
        return a+b;
    }
    console.log(add(1));    //2
    

    2、ES6中函数抛出错误

    function add(a,b=1){
        if(a == 1){
            throw new Error('a is a error');
        }
        return a+b;
    }
    console.log(add(1));     //Uncaught Error: a is a error
    

    通过throw new Error,控制台可以抛出我们自定义的错误

    3、函数中的严谨模式

    在ES5中,严谨模式'use strict'只能在文件的最上面,相当于全局使用,而在ES6中,可以写在函数中或者是块级作用域{}中,如下:

    function add(a,b=1){
        'use strict'
        return a+b;
    }
    console.log(add(1));    //Illegal 'use strict' directive in function with non-simple parameter list
    

    会报错是因为默认值与严谨模式冲突了,如果在函数中使用严谨模式,那么函数的参数就不能有默认值,将默认值去掉,程序就可以正常运行:

    function add(a,b){
        'use strict'
        return a+b;
    }
    console.log(add(1,2));      //3
    

    4、获得函数需要传递的参数

    在ES6中,可以通过函数名.length的方式获得函数需要传递的参数:

    function add(a,b){
        return a+b;
    }
    console.log(add.length);      //2
    

    但如果这时候我们给b一个默认值:

    function add(a,b=1){
        return a+b;
    }
    console.log(add.length);      //1
    

    所以通过函数名.length获得的就是函数必须要传的参数的个数,(不包括函数中有默认值的参数)

    5、箭头函数

    将上面的加函数变成箭头函数:

    var add = (a, b) => a+b;
    console.log(add(1,2));      //3
    

    如果函数体只有一句话就可以省略函数的花括号{},这时候就相当于{return a+b;},如果函数体里不止有一条语句,那么花括号{}不能省
    此外,箭头函数中不能使用new,所以,箭头函数不能当构造函数使用

    6、函数解构

    ①函数对对象的解构

    let json = {
        a:'字符串a',
        b:'字符串b'
    };
    function fun({a,b}){
        console.log(a,b);
    }
    fun(json);      //字符串a 字符串b
    

    通过该方式,json型的数据可以直接传到函数中,不用再一次次传参数了
    ②函数对数组的解构

    let arr = ['值1','值2','值3'];
    function fun(a,b,c){
        console.log(a,b,c);
    }
    fun(...arr);      //值1 值2 值3
    

    通过对象扩展运算符,可以在函数中传入数组,而不用传入一个一个的值

    7、ES6中的in

    ①判断对象中是否存在某个键名:

    let obj = {
        a:'值a',
        b:'值b'
    };
    console.log('a' in obj);      //true
    console.log('c' in obj);      //false
    

    如果存在,则返回true,否则返回false
    ②判断数组的值是否是空值
    在ES5中可以通过数组.length的方式看数组是否有值,但该方法有个弊端,如下:

    let arr = [,,,,,];
    console.log(arr.length);    //5
    

    当数组都是空值时,数组依旧有长度值
    针对这种情况,在ES6中就可以通过in的方式来判断

    let arr = [1,,,,];
    console.log(0 in arr);      //true
    console.log(1 in arr);      //false
    

    当数组在该位置上不是空值时,会返回true,如果是空值会返回false(这里的0和1代表的是数组的下标即索引)

    8、数组遍历的各种方式

    ①forEach:

    let arr = ['值1','值2','值3'];
    arr.forEach((item,index)=>console.log(index, item));
    //0 "值1"
    //1 "值2"
    //2 "值3"
    

    ②filter

    let arr = ['值1','值2','值3'];
    arr.filter((item,index)=>console.log(index, item));
    //0 "值1"
    //1 "值2"
    //2 "值3"
    

    ③some

    let arr = ['值1','值2','值3'];
    arr.some((item,index)=>console.log(index, item));
    //0 "值1"
    //1 "值2"
    //2 "值3"
    

    ④map

    let arr = ['值1','值2','值3'];
    arr.map(item => console.log(item));        //值1 值2 值3
    console.log(arr.map(item => 'web'));      //(3) ["web", "web", "web"]
    

    9、数组转字符串

    ①tostring()

    let arr = ['值1','值2','值3'];
    console.log(arr.toString());      //值1,值2,值3
    

    该方法转换时默认用逗号隔开
    ②join()

    let arr = ['值1','值2','值3'];
    console.log(arr.join('='));      //值1=值2=值3
    

    相关文章

      网友评论

          本文标题:ES6中的箭头函数以及扩展

          本文链接:https://www.haomeiwen.com/subject/mdgiyftx.html