Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ECMAScript Cookbook

You're reading from   ECMAScript Cookbook Over 70 recipes to help you learn the new ECMAScript (ES6/ES8) features and solve common JavaScript problems

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788628174
Length 348 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ross Harrison Ross Harrison
Author Profile Icon Ross Harrison
Ross Harrison
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Building with Modules FREE CHAPTER 2. Staying Compatible with Legacy Browsers 3. Working with Promises 4. Working with async/await and Functions 5. Web Workers, Shared Memory, and Atomics 6. Plain Objects 7. Creating Classes 8. Inheritance and Composition 9. Larger Structures with Design Patterns 10. Working with Arrays 11. Working with Maps and Symbols 12. Working with Sets 13. Other Books You May Enjoy

Exporting/importing multiple modules for external use

In the previous recipe, we loaded an ES module into an HTML page and executed an exported function. Now we can take a look at using multiple modules in a program. This allows us more flexibility when organizing our code.

Getting ready

Make sure you have Python installed and your browser properly configured.

How to do it...

  1. Create a new working directory, navigate into it with your command-line application, and start the Python SimpleHTTPServer.
  2. Create a file named rocket.js that exports the name of a rocket, a countdown duration, and a launch function:
export default name = "Saturn V"; 
export const COUNT_DOWN_DURATION = 10; 
 
 
export function launch () { 
  console.log(`Launching in ${COUNT_DOWN_DURATION}`); 
  launchSequence(); 
} 
 
function launchSequence () { 
  let currCount = COUNT_DOWN_DURATION; 
 
  const countDownInterval = setInterval(function () { 
    currCount--; 
 
    if (0 < currCount) { 
      console.log(currCount); 
    } else { 
      console.log('LIFTOFF!!! '); 
      clearInterval(countDownInterval); 
    } 
  }, 1000); 
}
  1. Create a file named main.js that imports from rocket.js, logs out details, and then calls the launch function:
import rocketName, {COUNT_DOWN_DURATION, launch } from './rocket.js'; 
 
export function main () { 
  console.log('This is a "%s" rocket', rocketName); 
  console.log('It will launch in  "%d" seconds.', COUNT_DOWN_DURATION); 
  launch(); 
} 
  1. Next, create an index.html file that imports the main.js module and runs the main function:
<html> 
  <head> 
    <meta charset='UTF-8' /> 
  </head> 
  <body> 
    <h1>Open your console.</h1> 
    <script type="module"> 
      import { main } from './main.js'; 
      main(); 
    </script> 
  </body> 
</html> 
  1. Open your browser and then the index.html file. You should see the following output:

How it works...

There are two options for exporting a member from a module. It can either be exported as the default member, or as a named member. In rocket.js, we see both methods:

export default name = "Saturn V"; 
export const COUNT_DOWN_DURATION = 10; 
export function launch () { ... } 

In this case, the string "Saturn V" is exported as the default member, while COUNT_DOWN_DURATION and launch are exported as named members. We can see the effect this has had when importing the module in main.js:

 import rocketName, { launch, COUNT_DOWN_DURATION } from './rocket.js'; 

We can see the difference in how the default member and the name members are imported. The name members appear inside the curly braces, and the name they are imported with matches their name in the module source file. The default module, on the other hand, appears outside the braces, and can be assigned to any name. The unexported member launchSequence cannot be imported by another module.

See also

  • Renaming imported modules
  • Nesting imported modules under a single namespace
You have been reading a chapter from
ECMAScript Cookbook
Published in: Mar 2018
Publisher: Packt
ISBN-13: 9781788628174
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime