箭头函数

165 阅读1分钟
  • 普通函数可以用于构造函数(new)。箭头函数不可用于构造函数,会报错。
  • 箭头函数没有自己的this,是当前上下文环境的this,不会被call()apply()修改this指向。
  • 由于 箭头函数没有自己的this指针,通过 call()或apply()方法调用一个函数时,只能传递参数(不能绑定this),他们的第一个参数会被忽略。
 // => 代表return后面的表达式
    let fn = (name) => name + ',hello!'

    // =>有多段表达式时,需要{}包裹,并且手动return
    //箭头函数只有一个参数,()可省略;多个或没有参数时,不可省
    // let fn = name => {
    //     let sentence = name + ',hello!'
    //     return sentence
    // }

    let a = fn('张无忌')
    console.log(a);
</script>