在javascript中创建util类

在javascript中创建util类,javascript,Javascript,我想为我的react应用程序创建一个util类。util类应该是静态类,正如我所知,它不能被实例化。那么,在javascript中创建util类的正确方法是什么?我们如何使其成为静态的?您可以定义一个只包含静态方法的类,它可能如下所示: class Util { static helper1 () {/* */} static helper2 () {/* */} } export default Util; 但是,由于您不需要实例化实用程序类的对象,因此您可能并不真正需要一个类

我想为我的react应用程序创建一个util类。util类应该是静态类,正如我所知,它不能被实例化。那么,在javascript中创建util类的正确方法是什么?我们如何使其成为静态的?

您可以定义一个只包含
静态方法的类,它可能如下所示:

class Util {

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

}

export default Util;
但是,由于您不需要实例化实用程序类的对象,因此您可能并不真正需要一个类和一个导出实用程序函数的简单模块来更好地满足您的需求:

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

export default {
  helper1,
  helper2
};