javascript - 如何在 node.js 的当前上下文中嵌入对象?
问题描述
我有一个 node.js 脚本文件。我需要一个具有静态函数的类。我想将类直接嵌入到脚本的当前上下文中,这样我就可以调用类的函数名而不引用类名。
在类文件中:
class SomeClass {
static doIt() {...}
}
在脚本文件中:
let cls = require('SomeClass');
// **** embed class into current context here ***
// Just call the function without class reference
doIt();
我以前见过这样做。在网上找不到如何做到这一点。
解决方案
您可以使用解构来做到这一点:
class SomeClass {
static doIt() {...}
static otherFunc() {...}
}
const { doIt, otherFunc } = SomeClass;
doIt();
推荐阅读
- postgresql - 为什么 Postgres 函数会抱怨语言子句不匹配错误?
- python - 为什么我的 VS Code 无法将“python”识别为内部或外部命令
- ruby-on-rails - 对于较大的视频,Chrome 或 Chromium 自动播放静音循环仅循环几次。如何让大型视频无限循环?
- android - 在颤振/飞镖中添加电话号码以联系
- java - Apache POI SXSSF 流式输出响应
- python - python解析日期字符串超出范围天数而没有错误
- javascript - onclick 函数中断而不是切换到新的 CSS 类
- python - 从 Dockerfile 运行 python 脚本
- python - Anaconda:在 tensorflow 环境中安装 opencv
- c# - 字符串属性中的默认值