admin管理员组文章数量:1419611
In a file called File_A.js
I have a function that contains a constant. I want to export this const, and only this one (not the whole function) in order to use the constant's value in another file called File_B.js
. I tried to use module.exports
but it returns that the variable is undefined. Below a simplified example. Thanks
// my function in File_A.js
const MyFunctionA = () => {
const myVariable = 'hello'
module.export = {myVariable: myVariable}
return (
/*...*/
);
}
// my second function in File_B.js
const MyFunctionB = () => {
const {myVariable} = require('./File_A.js');
console.log(myVariable) // undefined
return(
/*...*/
);
}
In a file called File_A.js
I have a function that contains a constant. I want to export this const, and only this one (not the whole function) in order to use the constant's value in another file called File_B.js
. I tried to use module.exports
but it returns that the variable is undefined. Below a simplified example. Thanks
// my function in File_A.js
const MyFunctionA = () => {
const myVariable = 'hello'
module.export = {myVariable: myVariable}
return (
/*...*/
);
}
// my second function in File_B.js
const MyFunctionB = () => {
const {myVariable} = require('./File_A.js');
console.log(myVariable) // undefined
return(
/*...*/
);
}
Share
Improve this question
edited Dec 2, 2020 at 11:15
rkhan
asked Dec 2, 2020 at 11:05
rkhanrkhan
3151 gold badge6 silver badges15 bronze badges
5
-
1
Isn't it
module.exports
(with ans
)? – T.J. Crowder Commented Dec 2, 2020 at 11:10 - The module.export should be in the global scope since it won't be assigned until MyFunctionA is called. – John Commented Dec 2, 2020 at 11:10
- 1 @PraveenKumarPurushothaman - Surprisingly, with the style of export the OP is using, you can...it's just not a good idea. :-) – T.J. Crowder Commented Dec 2, 2020 at 11:13
- 1 If it's a const reference to an immutable thing (in this case a string) just move it out of the function into the top level, there's no reason to hide it in a closure. – Jared Smith Commented Dec 2, 2020 at 11:19
- 1 @T.J.Crowder Thanks man. This is something new I learnt today. – Praveen Kumar Purushothaman Commented Dec 2, 2020 at 12:00
1 Answer
Reset to default 3how to export a constant which is inside a function?
There are two answers to this:
You don't. It doesn't make sense. Instead, you move the constant out of the function and export it.
You do it exactly as you have done, but the constant won't be in the module's exports until
MyFunctionA
has been executed at least once. This is possible because the CommonJS-style modules that you're using are dynamic and can change at runtime. However, making your exports dependent on a function call is asking for trouble, as you've discovered.
So taking #1 on board, we get:
// my function in File_A.js
const myVariable = "hello"; // Odd name for a constant? ;-)
module.exports.myVariable = myVariable;
const MyFunctionA = () => {
return (
/*...*/
);
};
A couple of notes on that:
MyFunctionA
still closes over the constant and references it exactly the way it used to.myVariable
doesn't bee a global, because the top-level scope of a CommonJS module isn't global scope.
本文标签: javascripthow to export a constant which is inside a functionReactJSStack Overflow
版权声明:本文标题:javascript - how to export a constant which is inside a function? - ReactJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745312338a2652990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论