Using more import/export possibilities

In the previous section, we exported a single item from our module by using what is called a default export: one per module. There is also another kind of export, named exports, of which you can have several per module. You can even mix them in the same module, but it's usually clearer to not mix them up. For example, say you needed a module to do some distance and weight conversions. Your module could be as follows:

// Source file: src/module_conversions.js

/* @flow */

type conversion = number => number;

const SPEED_OF_LIGHT_IN_VACUUM_IN_MPS = 186282;
const KILOMETERS_PER_MILE = 1.60934;
const GRAMS_PER_POUND = 453.592;
const GRAMS_PER_OUNCE = 28.3495;

const milesToKm: conversion = m => m * KILOMETERS_PER_MILE;
const kmToMiles: conversion = k => k / KILOMETERS_PER_MILE;

const poundsToKg: conversion = p => p * (GRAMS_PER_POUND / 1000);
const kgToPounds: conversion = k => k / (GRAMS_PER_POUND / 1000);

const ouncesToGrams: conversion = o => o * GRAMS_PER_OUNCE;
const gramsToOunces: conversion = g => g / GRAMS_PER_OUNCE;

/*
It's usually preferred to include all "export"
statements together, at the end of the file.
You need not have a SINGLE export, however.
*/
export { milesToKm, kmToMiles };
export { poundsToKg, kgToPounds, gramsToOunces, ouncesToGrams };
export { SPEED_OF_LIGHT_IN_VACUUM_IN_MPS };

You can have as many definitions as you want, and you can export any of them; in our case, we are exporting six functions and one constant. You do not need to pack everything into a single export; you can have several, as we have already shown you. Exports are usually grouped together at the end of a module to help a reader quickly find everything that the module exports, but sometimes you may find them all throughout the code; we won't be doing that. You can also export something in the same line where you define it, as in export const LENGTH_OF_YEAR_IN_DAYS = 365.2422, but we won't use that style either, for consistency.

When importing a module with named exports, you just have to say which of the exports you want. You can import from different modules; you'll just require several import statements. It's a standard practice to group all of them at the start of your source file. You can also rename an import, as in the case of poundsToKg in the following code, which we'll use as p_to_kg. In reality, you would do this if you had identically named imports from two different modules; in our particular example, it doesn't really make sense:

// Source file: src/module_conversion_usage.js

/* @flow */

import {
milesToKm,
ouncesToGrams,
poundsToKg as p_to_kg
} from "./module_conversions.js";

console.log(`A miss is as good as ${milesToKm(1)} kilometers.`);

console.log(
`${ouncesToGrams(1)} grams of protection `,
`are worth ${p_to_kg(1) * 1000} grams of cure.`
);

So far, we have seen how to export JS elements—functions and constants in our example—but you could also export classes, objects, arrays, and so on. In the next section, we'll get back to Flow, and see how types can also be exported and imported.

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

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