ES6 has introduced block scopes using let
and const
. What's the correct way to export
data from within the block scope, so that I can access it from outside. Using IIFE you could just assign a the expression to a variable.
{
const add = (a, b) => a+b
// export add?
}
Using export
within the block throws an error.
'import' and 'export' may only appear at the top level
One way I've found is to assign it to the global window
object
{
const add = (a, b) => a+b
window.add = add
}
I'm just curious to know what the suggested way to do this is?
Create a function, and return the value you need.
const Add = () => { return 'function add returned...'; };
You can also create a variable outside of the block and just assign it a value inside the block.
let myVar;
const myFunc = () => {
myVar = 'foo';
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.