ES6(一)

作者: 阿昕_ | 来源:发表于2018-06-28 20:37 被阅读13次

    声明变量

    • let
      -- 不允许重复声明
      -- 不存在变量提升
      -- 块级作用域
    • const
      -- 只读常量
      -- 其他同let

    解构赋值

    • 数组
    let [a,b] = [1,2]
    let [a,b,c=3] = [1,2]   //默认值
    
    • 对象
    let {name,age} = {name:1,age:2}
    let {name,age=2} = {name:1}   //默认值
    let {name:N,age} = {name:1,age:2}  //别名
    
    • 字符串
     let [a,b,c] = '123'
    

    -函数参数

    function fn({a,b='3'}){
        console.log(a,b)
    }
    
    fn({
      a:1
    })
    

    模板字符串

    let a = 'aaa';
    let str1 = `12345${a}678`;
    let str2 = `12345${ true ? 'YYY' : 'NNN'}678`;
    

    箭头函数

    • ( ) => { }
    • this为定义时所在作用域的this
    • 无法使用arguments对象
    • 不可以当做构造函数

    函数扩展

    • 参数默认值
    • rest参数(a,...arr):剩余实参
    function foo(a,...arr){
      console.log(a)
      console.log(arr)
    }
    foo(1);
    foo(1,2);
    foo(1,2,3);
    foo(1,2,3,4);
    

    对象扩展

    • 简写
    let a = 20;
    let o = {
        a:a,
        fn:function(){}
    }
    
    写成:
    
    let a = 20;
    let o = {
        a,
        fn(){}
    }
    
    • Object.assign() 合并对象
    let a = {a:1}
    let b = {a:11,b:2}
    let c = {c:3}
    
    Object.assign(a,b,c)
    
    function foo(option){
        let defaultOption = {
            a:'默认a',
            b:'默认b',
            c:'默认c',
        };
        Object.assign(defaultOption,option);
        
        console.log(defaultOption); //{a: 1, b: "默认b", c: 3}
    }
    
    foo({
        a:1,
        c:3
    })
    

    扩展运算符(...)

    let a = [1,2,3]
    let b = [4,5,6]
    let c = [...a,...b]
    
    let d = {a:1}
    let e = {b:2,c:3}
    let f = {...d,...e}
    

    map、filter、find

    let arr = [1,2,3];
    arr.forEach(function(item,index){
        console.log(item,index);
    })
    
    //返回一个运算过的新数组
    let newArr = arr.map(function(item,index){
        console.log(item,index);
        return item*2;
    })
    console.log(newArr)
    
    //返回一个过滤后的新数组
    let filterNewArr = arr.filter(function(item,index){
        console.log(item,index);
        return item%2===0;
    })
    console.log(filterNewArr)
    
    //返回符合条件的第一项
    let findItem = arr.find(function(item,index){
        console.log(item,index);
        return item<3;
    })
    console.log(findItem)
    

    相关文章

      网友评论

          本文标题:ES6(一)

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