Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript (ES6) Modules: Is it possible to export a variable with a dynamic name? [duplicate]

In ES6 I can export a simple foo constant:

export const foo = 1;

I can also convert the value of that export (1) to a variable, and export that:

const fooValue = 1;
export foo = fooValue;

But my question is, is there any way I can convert the key of the export (foo) in to a variable:

const fooLabel = 'foo';
const fooValue = 1;
export something(fooLabel) = fooValue;

Or do exports always have to explicitly named?

like image 576
machineghost Avatar asked Sep 26 '16 17:09

machineghost


1 Answers

You won't be able to use named exports. It's easy enough to export a single object with dynamically generated keys though:

let obj = {};

obj[fooLabel] = fooValue;

export default obj;
like image 190
Justin Niessner Avatar answered Oct 12 '22 18:10

Justin Niessner