ECMAScript6 扩展运算符

【扩展运算符】spread
作用:序列化 对象序列化是指将对象的状态转换为字符串
通过扩展运算符实现assign操作

    var obj1 = {a:1};
    var obj2 = {b:2};
    var obj3 = {...obj1,...obj2};
    console.log(obj3); //Object { a: 1, b: 2 }

rest参数(...) 扩展运算符

//1.提取数据
function a(){
  var arr = [...arguments];
  console.log(arr) //Array(3) [ 1, 2, 3 ]
}
a(1,2,3);

//2.剩余参数
function b(a,...r){
  console.log(r); //Array(3) [ 2, 3, 4 ]
}
b(1,2,3,4);

function fn3(a,...arr){
    console.log(a);  //1
    console.log(arr);  //Array(4) [ 2, 3, 4, 5 ]
}
fn3(1,2,3,4,5);

解构
1.数组数据解构

var [a,b,c,d] = [1,2,3,4];
console.log(c); //3

2.对象数据解构

var person = (function(){
  var sex  = 'man';
  function showit(){
    console.log('he is a '+sex );  
  }
  return {
    sex,showit
  }
})();
var {sex,showit} = person;
console.log(sex); //man
console.log(showit()); // he is a man

模板字符串, 字符拼接以及换行更加方便 简洁

var txt = '我是个学生';
var txt2 = '自动化控制专业';
var str = `
  • ${ txt }
  • ${ txt2 }
`;

函数的扩展
1.函数默认值

function showIt(num){
  var num = num || 10;
  console.log(num);  //10
};
showIt(0); //数字0解析为false,所以永远走或的另一测,永远为10

function showIt(num=10){
  console.log(num);
};
showIt(0); //0  添加了默认值后,可以正常输出0
showIt(); //10 不写,则走默认值

3.函数简写

var obj = {fn(){
    //something
}}
//同等于
var obj = {
  fn: function fn() {}
};

你可能感兴趣的:(ECMAScript6 扩展运算符)