JS Refactoring Combo: Inline Function as Method

Lars Grammel - May 28 '22 - - Dev Community

Sometimes functions are only used as property values. In such cases, you can convert the functions into methods.

Before (Example)

function aFunction(aParameter) {
  doSomething(aParameter);
}

const anObject = {
  aMethod: aFunction
};
Enter fullscreen mode Exit fullscreen mode

Refactoring Steps

Inline Named Function as Method

💡  The refactoring steps are using P42 JavaScript Assistant v1.109

  1. Convert the named function into a variable that contains the function expression
  2. Inline the variable
  3. Convert the function to an object method

After (Example)

const anObject = {
  aMethod(aParameter) {
    doSomething(aParameter);
  }
};
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .