ECMAScript Cookbook
上QQ阅读APP看书,第一时间看更新

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: