javascript - how to export a constant which is inside a function? - ReactJS - Stack Overflow

In a file called File_A.js I have a function that contains a constant. I want to export this const, and

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 an s)? – 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
Add a ment  | 

1 Answer 1

Reset to default 3

how to export a constant which is inside a function?

There are two answers to this:

  1. You don't. It doesn't make sense. Instead, you move the constant out of the function and export it.

  2. 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:

  1. MyFunctionA still closes over the constant and references it exactly the way it used to.

  2. myVariable doesn't bee a global, because the top-level scope of a CommonJS module isn't global scope.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745312339a4622055.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信