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.js
export const PI = 3.14
export const sqrt3 = 1.73
export function returnWhatYouSay(text) { return text; }

Let's import everything at once:

// index.js
import * as myModule from './module.js'
console.log(myModule.PI) // 3.14
console.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 gather all the information about the import/export syntax in the following sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.35.244