ES6 函数扩展 箭头函数

217 阅读2分钟

ES6 函数扩展 箭头函数

1.函数扩展

默认参数与不定参数

1.默认参数

基本用法

 function fn(name,age=17){
     console.log(name+","+age);
    }
    fn("Amy",18);  // Amy,18
    fn("Amy","");  // Amy,
    fn("Amy");     // Amy,17
    
    注意点:使用函数默认参数时,不允许有同名参数。
    // 不报错
    function fn(name,name){
     console.log(name);
    }
    
    // 报错
    //SyntaxError: Duplicate parameter name not allowed in this context
    function fn(name,name,age=17){
     console.log(name+","+age);
    }
    只有在未传递参数,或者参数为 undefined 时,才会使用默认参数,null 值被认为是有效的值传递。
    function fn(name,age=17){
        console.log(name+","+age);
    }
    fn("Amy",null); // Amy,null

函数参数默认值存在暂时性死区,在函数参数默认值表达式中,还未初始化赋值的参数值无法作为其他参数的默认值。

2.不定参数

​ 不定参数用来表示不确定参数个数,形如,…变量名,由…加上一个具名参数标识符组成。具名参数只能放在参数组的最后,并且有且只有一个不定参数

基本用法:

 function f(...values){
        console.log(values.length);
    }
    f(1,2);      //2
    f(1,2,3,4);  //4
2.箭头函数

箭头函数提供了一种更加简洁的函数书写方式。

基本语法:

参数 => 函数体

function 函数名称(参数列表){
函数执行体
}
this指针的固化

var 函数名称 = (参数列表)=>{
函数执行体
}

基本用法:

var f = v => v;
    //等价于
    var f = function(a){
     return a;
    }
    f(1);  //1

当箭头函数没有参数或者有多个参数,要用 () 括起来

var f = (a,b) => a+b;
    f(6,2);  //8

当箭头函数函数体有多行语句,用 {} 包裹起来,表示代码块,当只有一行语句,并且需要返回结果时,可以省略 {} , 结果会自动返回。

   var f = (a,b) => {
     let result = a+b;
     return result;
    }
    f(6,2);  // 8

单行语句返回形式

当箭头函数要返回对象的时候,为了区分于代码块,要用 () 将对象包裹起来

 // 报错
    var f = (id,name) => {id: id, name: name};
    f(6,2);  // SyntaxError: Unexpected token :
    
    // 不报错
    var f = (id,name) => ({id: id, name: name});
    f(6,2);  // {id: 6, name: 2}

注意:没有 this、super、arguments 和 new.target 绑定

 var func = () => {
      // 箭头函数里面没有 this 对象,
      // 此时的 this 是外层的 this 对象,即 Window
      console.log(this)
    }
    func(55)  // Window
    
    var func = () => {    
      console.log(arguments)
    }
    func(55);  // ReferenceError: arguments is not defined

箭头函数体中的 this 对象,是定义函数时的对象,而不是使用函数时的对象。

 function fn(){
      setTimeout(()=>{
        // 定义时,this 绑定的是 fn 中的 this 对象
        console.log(this.a);
      },0)
    }
    var a = 20;
    // fn 的 this 对象为 {a: 19}
    fn.call({a: 18});  // 18
    
    var a = {
      a: 18,
      fn:function(){
        setTimeout(()=>{
          // 定义时,this 绑定的是 fn 中的 this 对象
          console.log(this.a);
        },0)
      }
    }
    
    // call():
    //将该函数的定义放到传入的对象下
    //调用

不可以作为构造函数,也就是不能使用 new 命令,否则会报错