拡張オペレータと解構の簡単な実用化

3576 ワード

拡張オペレータ(...)
function showMe(name,age)
{
  return "   "+name+"   "+age;
}

var name = "zhangsan";
var age = 18;
console.log(showMe(name,18));//   zhangsan   18
// 
var me = ["jack",20];
console.log(showMe(...me));//   jack   20

ES 2015の書き方に変更:
let showMe = (name,age)=>`${name}    ${age}`;

//
let me = ["lily",18];
console.log(showMe(...me));//lily    18

[拡張](Extend)オペレータを結合解除するには
let [sex,...me] = [" ","jack",18];
console.log(me.length);//2
//   me       ,     "jack",18

デフォルト
var [name="jack",age=18] = [];
//    2       ,  name age     
//      
let getName = ()=> {
  //       ,       ,     undefined
  return undefined;
}
let [sex=" ",name="jack",age=18] = [,getName(),];
console.log(name);//jack

falseを返すと
//      
let getName = ()=> {
  //       ,       ,     undefined
  return false;
}
let [sex=" ",name="jack",age=18] = [,getName(),];
console.log(name);//       :false