Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a util class in javascript

Tags:

javascript

I want to create a util class for my react app. util classes should be static classes which cannot be instantiated as I know. So, what is the proper way to create a util class in javascript and how we can make it static?

like image 557
Shashika Avatar asked Dec 23 '22 01:12

Shashika


1 Answers

You can define a class that contains only static methods, it may look something like as follows:

class Util {

  static helper1 () {/* */}
  static helper2 () {/* */}

}

export default Util;

However, since you don't need a possibility to instantiate an object of the utility class, chances are that you don't really need a class and a simple module that exports utility functions will suite your demand better:

const helper1 = () => {/* */};
const helper2 = () => {/* */};

export default {
  helper1,
  helper2
};
like image 193
antonku Avatar answered Dec 25 '22 16:12

antonku