es6 的函数扩展

// es6函数扩展
{
  function test(x, y = 'world'){
    console.log('默认值',x,y);
  }
  test('hello');  // 默认值  hello world
  test('hello','bill'); // 默认值  hello bill
}

{
  let x='test';
  function test2(x,y=x){
    console.log('作用域',x,y);
  }
  test2('bill'); // 作用域  bill  bill

  test3(); // 作用域  undifined undifined
}

{
    let x='test';
    function test2(c,y=x){
        console.log('作用域',x,y);
    }
    test('bill'); // 作用域  bill  test

}

// 将输入的值都转换成数组
{
  function test3(...arg){
    for(let v of arg){
      console.log(v);
    }
  }
  test3(1,2,3,4,'a'); // [1,2,3,4,a]
}

{
  // 将数组转成一个离散的值
  console.log(...[1,2,4]); // 1 2 4
  console.log('a',...[1,2,4]); // a 1 2 4
}

{
  // 箭头函数
  // 函数名  函数参数  返回值
  let arrow = v => v*2;
  let arrow2 = () => 5;
  console.log('arrow',arrow(3)); // arrow  6
  console.log(arrow2());  //  5

}


//  尾调用:
{
  function tail(x){
    console.log('tail',x);
  }
  function fx(x){
    return tail(x)
  }
  fx(123)  // tail 123
}

猜你喜欢

转载自blog.csdn.net/luzhaopan/article/details/81914669