DEV Community

Cover image for JS Refactoring Combo: Inline Function as Method
Lars Grammel for P42

Posted on • Edited on • Originally published at p42.ai

2

JS Refactoring Combo: Inline Function as Method

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

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (1)

Collapse
 
evasteps profile image
Eva Lam

That’s cool. Haven’t thought of this way of writing. Thanks so much for sharing. Insightful ! And the series allows me to learn how to write cleaner code

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay