Single-Line to Multi-Line ES6 Fat Arrow Function?

前端 未结 2 1971
南笙
南笙 2020-12-16 11:43

I\'m learning ES6 fat arrow functions. What is the correct way to change this code, so as to be able to put another line, even const a = 100; in the place indic

相关标签:
2条回答
  • 2020-12-16 12:05

    If you want to convert the following method into having more lines:

    {
      filter: appointment => true
    }
    

    You have to add curly braces and a return statement:

    {
      filter: appointment => {
        // ... add your other lines here
        return true;
      }
    }
    
    0 讨论(0)
  • 2020-12-16 12:09
    filter: appointment => true,
    ...
    

    is (and parentheses aren't needed around true) a shortcut for

    filter: appointment => {
      return true;
    },
    ...
    

    which is a shortcut for

    filter: function (appointment) {
      return true;
    }.bind(this),
    ...
    

    Any amount of lines can be added before return statement.

    0 讨论(0)
提交回复
热议问题