Modules Flashcards
What are modules?
Modules are reusable pieces of code that can be exported from one program and imported for use in another program.
What are the steps to create/export a module? With these steps in mind, write an example of an object and then export it a module.
- Define an object to represent the module
- Add data or behavior to the module
- Export the module, like so: module.exports = Menu;
let Airplane = {}; Airplane.myAirplane = 'Starjet'; module.exports = Airplane;
How would you require this Airplane module in order to include in a function that logs the property to the console?
const Airplane = require(‘./1-airplane.js’);
const displayAirplane = () => {
console.log(Airplane.myAirplane);
}
displayAirplane();
Create an empty object and then assign a property with a value when you export it.
const Airplane = {}; module.exports = { myAirplane: 'CloudJet', displayAirplane ()function { return this.myAirplane; } };
There are two new more readale/easier ways to export a module, what are these two and write an example for each. How would you import if you used the named export method?
The two way are default exports and named exports.
default export: let Menu = {}; export default Menu;
named export:
export {availableAirplanes, flightRequirements, meetsStaffRequirements};
named import:
import {availableAirplanes, flightRequirements, meetsStaffRequirements} from ‘./airplane’;
How could you export a named export? So modify the following lines of code.
let specialty = ''; function isVegetarian() { }; function isLowSodium() { };
export let specialty = ''; export function isVegetarian() { }; export function isLowSodium() { };
When exporting a module, you can export it as a different name, export the following:
let specialty = ''; function isVegetarian() { }; function isLowSodium() { };
as different names
let specialty = ''; function isVegetarian() { }; function isLowSodium() { };
export {speciality as specials, isVegetarian as isVeg, isLowSodium as isLowSod}