所以我想让我的 index.js 看起来更干净,所以我认为它会更干净,如果命令(switch 语句)在另一个文件中,所以现在我想知道我是否可以将它们放在一个 json 文件中,或者(如果不可能)在另一个 js 文件中,然后以某种方式将其导入 index.js
你不能在 JSON 文件中做到这一点,但你可以在 js 文件中做到这一点。
下面是一个例子:
1- 创建一个名为 helper.js 的文件
2-在其中添加此代码:
module.exports = {
// example function
biggerThanFive: array => {
return array.filter(elem => {
return elem > 5;
});
}
}
3- 在 index.js 文件中需要这样的帮助文件:
const helpers = require("./helper");
4- 调用函数并给它一个这样的值:
helper.biggerThanFive([1, 2, 3, 4, 5, 6, 7, 8, 9])
// [6, 7, 8, 9]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句