Wildcard imports

What if you want to import all the exported entities in the whole module? Writing each entity's name yourself is cumbersome; also, if you do that, you'll pollute the global scope. Let's see how we can fix both of these issues.

Let's assume our module.js looks something like this:

// module.jsexport const PI = 3.14export const sqrt3 = 1.73export function returnWhatYouSay(text) { return text; }

Let's import everything at once:

// index.jsimport * as myModule from './module.js'console.log(myModule.PI) // 3.14console.log(myModule.returnWhatYouSay("This is cool!"))

The asterisk (*) will import everything that is exported under the scope of the myModule object. It makes accessing all exported variables/methods cleaner.

Let's quickly ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.