JavaScript 箭头函数

x33g5p2x  于2022-04-29 转载在 Java  
字(2.8k)|赞(0)|评价(0)|浏览(609)

在本教程中,您将借助示例了解 JavaScript 箭头函数。
    箭头函数是 ES6 版本 JavaScript 中引入的特性之一。与常规函数相比,它允许您以更简洁的方式创建函数。例如,
    这个函数:

  1. // function expression
  2. let x = function(x, y) {
  3. return x * y;
  4. }

可以写成:

  1. // using arrow functions
  2. let x = (x, y) => x * y;

使用箭头函数。

箭头函数语法

箭头函数的语法是:

  1. let myFunction = (arg1, arg2, ...argN) => {
  2. statement(s)
  3. }

这里,

  • myFunction 是函数的名称
  • arg1, arg2, …argN 是函数参数
  • statement(s) 是函数体

如果主体有单个语句或表达式,您可以将箭头函数编写为:

  1. let myFunction = (arg1, arg2, ...argN) => expression
示例 1:无参数的箭头函数

如果一个函数不带任何参数,那么你应该使用空括号。例如,

  1. let greet = () => console.log('Hello');
  2. greet(); // Hello
示例 2:带一个参数的箭头函数

如果函数只有一个参数,则可以省略括号。例如,

  1. let greet = x => console.log(x);
  2. greet('Hello'); // Hello
示例 3:作为表达式的箭头函数

您还可以动态创建函数并将其用作表达式。例如,

  1. let age = 5;
  2. let welcome = (age < 18) ?
  3. () => console.log('Baby') :
  4. () => console.log('Adult');
  5. welcome(); // Baby
示例 4:多行箭头函数

如果函数体有多条语句,则需要将它们放在花括号 { } 中。例如,

  1. let sum = (a, b) => {
  2. let result = a + b;
  3. return result;
  4. }
  5. let result1 = sum(5,7);
  6. console.log(result1); // 12
this 与箭头函数

在常规函数中,this 关键字指的是调用它的函数。
    但是,this 与箭头函数无关。箭头函数没有自己的 this。因此,无论何时调用 this,它都指的是其父范围。例如,

在常规函数中
  1. function Person() {
  2. this.name = 'Jack',
  3. this.age = 25,
  4. this.sayName = function () {
  5. // this is accessible
  6. console.log(this.age);
  7. function innerFunc() {
  8. // this refers to the global object
  9. console.log(this.age);
  10. console.log(this);
  11. }
  12. innerFunc();
  13. }
  14. }
  15. let x = new Person();
  16. x.sayName();

输出

  1. 25
  2. undefined
  3. Window {}

在这里,this.sayName() 内的 this.age 是可访问的,因为 this.sayName() 是对象的方法。
    然而,innerFunc() 是一个普通函数,this.age 是不可访问的,因为 this 指的是全局对象(浏览器中的窗口对象)。因此,innerFunc() 函数内部的 this.age 给出 undefined。

在箭头函数中
  1. function Person() {
  2. this.name = 'Jack',
  3. this.age = 25,
  4. this.sayName = function () {
  5. console.log(this.age);
  6. let innerFunc = () => {
  7. console.log(this.age);
  8. }
  9. innerFunc();
  10. }
  11. }
  12. const x = new Person();
  13. x.sayName();

输出

  1. 25
  2. 25

这里,innerFunc() 函数是使用箭头函数定义的。在箭头函数中,this 指父函数的作用域。因此,this.age 给出 25。

参数绑定

常规函数具有参数绑定。这就是为什么当您将参数传递给常规函数时,您可以使用 arguments 关键字访问它们。例如,

  1. let x = function () {
  2. console.log(arguments);
  3. }
  4. x(4,6,7); // Arguments [4, 6, 7]

箭头函数没有参数绑定。
    当您尝试使用箭头函数访问参数时,它会给出错误。例如,

  1. let x = () => {
  2. console.log(arguments);
  3. }
  4. x(4,6,7);
  5. // ReferenceError: Can't find variable: arguments

要解决这个问题,可以使用扩展语法。例如,

  1. let x = (...n) => {
  2. console.log(n);
  3. }
  4. x(4,6,7); // [4, 6, 7]
带有 Promise 和回调的箭头函数

箭头函数提供了更好的语法来编写 Promise 和回调。例如,

  1. // ES5
  2. asyncFunction().then(function() {
  3. return asyncFunction1();
  4. }).then(function() {
  5. return asyncFunction2();
  6. }).then(function() {
  7. finish;
  8. });

可以写成

  1. // ES6
  2. asyncFunction()
  3. .then(() => asyncFunction1())
  4. .then(() => asyncFunction2())
  5. .then(() => finish);
使用箭头函数应该避免的事情
  1. 你不应该使用箭头函数在对象内部创建方法。
  1. let person = {
  2. name: 'Jack',
  3. age: 25,
  4. sayName: () => {
  5. // this refers to the global .....
  6. //
  7. console.log(this.age);
  8. }
  9. }
  10. person.sayName(); // undefined
  1. 不能使用箭头函数作为构造函数。例如,
  1. let Foo = () => {};
  2. let foo = new Foo(); // TypeError: Foo is not a constructor

注意:箭头函数是在 ES6 中引入的。某些浏览器可能不支持使用箭头功能。访问JavaScript 箭头函数支持以了解更多信息。

上一教程 :JS ES6                                          下一教程 :JS Default Parameters

参考文档

[1] Parewa Labs Pvt. Ltd. (2022, January 1). Getting Started With JavaScript, from Parewa Labs Pvt. Ltd: https://www.programiz.com/javascript/arrow-function

相关文章