Javascript 使用在函数中定义的函数从另一个文件导出
我在helperFunctions.js文件中有一些类似的代码:Javascript 使用在函数中定义的函数从另一个文件导出,javascript,node.js,Javascript,Node.js,我在helperFunctions.js文件中有一些类似的代码: exports helperFunctions = () => { const functionA = async(args) => { console.log(args); }; const functionB = async(args) => { functionA(myArg); }; } 如何从单独的文件(例如main.js)中调用func
exports helperFunctions = () => {
const functionA = async(args) => {
console.log(args);
};
const functionB = async(args) => {
functionA(myArg);
};
}
如何从单独的文件(例如main.js)中调用function
和functionB
我试过:
import { helperFunctions } from './helperFunctions';
//...some code
helperFunctions.functionA('hello');
// OR
functionA('hello');
具体错误是:
TypeError: _helperFunctions.helperFunctions.functionA is not a function
在尝试第二种解决方案时,它是:
ReferenceError: functionA is not defined
我试图避免逐字逐句地导入我正在使用的每个函数(通过导出我正在使用的每个函数)。我想为我需要的函数做一些类似于helperFunctions.function的事情。它真的需要一个函数吗?您可以导出一个对象:
//helperFunctions.js
让helperFunction={
函数:异步(参数)=>{
console.log(args);
},
函数B:async(args)=>{
功能性(myArg);
}
}
导出助手功能;
你不能。这些函数是导出的单个匿名=>
函数中的局部变量值。它们不能在该上下文外部使用,除非有更多代码(您没有发布)以某种方式显式地使它们可用。您不应该使它们成为本地函数。您应该导出一个对象并使这些函数成为该对象的属性。helperFunctions
可能不应该是函数,而应该是对象。