javascript - (Javascript)如何将文件/一堆代码转换为变量?
问题描述
假设我有一个 helloworld.js 和一个 test.js 文件。在 test.js 文件中,我想创建一个 const:
const helloworldFile = *contents of helloworld.js* file.
我该怎么做?
例如 helloworld.js 的内容是:
console.log(1+1)
怎么做
const helloworldFile = console.log(1+1)
解决方案
如果您想在其他地方使用一个文件中的函数,您可以从该文件中导出它,如下所示:
export const someFunction = () => console.log(1 + 1)
并将其导入到顶部的另一个文件中,如下所示:
import { someFunction } from './helloworld.js' // or whatever the relative path to the file is
然后,您可以在该文件中的任何位置使用该函数。这就是你想要做的吗?
推荐阅读
- java - 如何在Java中的exe文件末尾写一个字符串
- python - 简化 Python 中的一系列 elif 语句
- c# - Stripe PaymentIntent 键未定义 | 无法从控制器传递 ClientSecret 密钥 | ASP.NET MVC
- r - 用样本 ID 标记 ggplot2 箱线图点
- c++ - 除了遗产之外,这个“:”运算符是什么意思?- 使用字符串到字符的转换
- r - 制作具有许多相同向量的倍数作为其列的数据框的简洁方法是什么?
- ms-access - MS Access - 压缩和修复大型数据库
- node.js - 我收到一条错误消息,说 model.findOne() 不起作用
- xml - 使用 VBA 解析 XML
- css - CSS:普遍适用位置:相对