Book Image

ECMAScript Cookbook

By : Ross Harrison
Book Image

ECMAScript Cookbook

By: Ross Harrison

Overview of this book

ECMAScript Cookbook follows a modular approach with independent recipes covering different feature sets and specifications of ECMAScript to help you become an efficient programmer. This book starts off with organizing your JavaScript applications as well as delivering those applications to modem and legacy systems. You will get acquainted with features of ECMAScript 8 such as async, SharedArrayBuffers, and Atomic operations that enhance asynchronous and parallel operations. In addition to this, this book will introduce you to SharedArrayBuffers, which allow web workers to share data directly, and Atomic operations, which help coordinate behavior across the threads. You will also work with OOP and Collections, followed by new functions and methods on the built-in Object and Array types that make common operations more manageable and less error-prone. You will then see how to easily build more sophisticated and expressive program structures with classes and inheritance. In the end, we will cover Sets, Maps, and Symbols, which are the new types introduced in ECMAScript 6 to add new behaviors and allow you to create simple and powerful modules. By the end of the book, you will be able to produce more efficient, expressive, and simpler programs using the new features of ECMAScript. ?
Table of Contents (20 chapters)
Title Page
Copyright and Credits
Dedication
PacktPub.com
Contributors
Preface
Index

Renaming imported modules


Modules allow more flexibility in organizing code. This allows for a shorter, more contextual name. For example, in the previous recipe, we named a function launch instead of something more verbose such as launchRocket. This helps keep our code more readable, but it also means that different modules can export members that use the same name.

In this recipe, we'll rename imports in order to avoid these namespace collisions.

Getting ready

We'll be reusing the code from the previous recipe (Exporting/importing multiple modules for external use). The changes from the previous files will be highlighted.

How to do it...

  1. Copy the folder created for the previous recipe into a new directory.
  2. Navigate to that directory with your command-line application and start the Python server.
  1. Rename rocket.js to saturn-v.js, add the name of the rocket to the log statements, and update the main.js import statement:
// main.js 
import name, { launch, COUNT_DOWN_DURATION } from './saturn-v.js'; 
 
export function main () { 
  console.log('This is a "%s" rocket', name); 
  console.log('It will launch in  "%d" seconds.', COUNT_DOWN_DURATION); 
  launch(); 
} 
// saturn-v.js 
export function launch () { 
  console.log(`Launching %s in ${COUNT_DOWN_DURATION}`, name); 
  launchSequence(); 
} 
 
function launchSequence () { 
  // . . .  
      console.log(%shas LIFTOFF!!!
', name); // . . . }
  1. Copy saturn-v.js to a new file named falcon-heavy.js and change the default export value and the COUNT_DOWN_DURATION:
export default name = "Falcon Heavy";export const COUNT_DOWN_DURATION = 5;
  1. Import the falcon module into main.js. Rename the imported members to avoid conflicts and launch the falcon rocket as well:
import rocketName, { launch, COUNT_DOWN_DURATION } from './saturn-v.js'; 
import falconName, { launch as falconLaunch, COUNT_DOWN_DURATION as falconCount } from './falcon-heavy.js'; 
 
export function main () { 
  console.log('This is a "%s" rocket', rocketName); 
  console.log('It will launch in  "%d" seconds.', COUNT_DOWN_DURATION); 
  launch(); 
   
  console.log('This is a "%s" rocket', falconName);  console.log('It will launch in  "%d" seconds.', falconCount);  falconLaunch(); 
} 
  1. Open index.html in your browser and you should see the following output:

How it works...

When we duplicated the saturn-v.js file to and imported the members from falcon-heavy.js, we had a potential namespace conflict. Both files export members named COUNT_DOWN_DURATION and launch. But using the as keyword, we renamed those members in order to avoid that conflict. Now the importing main.js file can use both sets of members without issue.

Renaming members can also be helpful to adding context. For example, it might be useful to rename the launch as launchRocket even if there is no conflict. This give the importing module additional context, and makes the code a bit clearer.