javascript - 在 javascript 中创建一个 util 类
问题描述
我想为我的反应应用程序创建一个实用程序类。util 类应该是静态类,据我所知不能实例化。那么,在 javascript 中创建 util 类的正确方法是什么,以及我们如何使它成为静态的?
解决方案
您可以定义一个只包含static
方法的类,它可能如下所示:
class Util {
static helper1 () {/* */}
static helper2 () {/* */}
}
export default Util;
但是,由于您不需要实例化实用程序类的对象,因此您可能并不需要一个类,并且导出实用程序函数的简单模块将更好地满足您的需求:
const helper1 = () => {/* */};
const helper2 = () => {/* */};
export default {
helper1,
helper2
};
推荐阅读
- python - Selenium Python遍历Web表格列,条件为真后停止,如果条件为假则抛出错误
- xcode - Xcode 故事板的幕后发生了什么?
- django - 如何自动测试或检查 __str__ 中使用的属性是否存在于我的 Django 模型的类中?
- python-3.x - 可变定位的图像标准化
- android - 如何在 NativeScript 中使用原生 API?
- python - Python pandas - 根据缺少的区域代码删除分组的行
- python - 努力看看为什么反向排序不考虑 100
- java - Sublime 3 - 特定项目的 Java 构建系统
- hash - 在 SAS v python 代码和 CSV 文件中创建的数据集 md5 哈希的差异
- python-3.x - 无法从 databricks pyspark 工作人员写入 Azure Sql DataWarehouse