Modules Flashcards

1
Q

What are modules?

A

Modules are reusable pieces of code that can be exported from one program and imported for use in another program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A
  1. Define an object to represent the module
  2. Add data or behavior to the module
  3. Export the module, like so: module.exports = Menu;
let Airplane = {};
Airplane.myAirplane = 'Starjet';
module.exports = Airplane;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you require this Airplane module in order to include in a function that logs the property to the console?

A

const Airplane = require(‘./1-airplane.js’);

const displayAirplane = () => {
console.log(Airplane.myAirplane);
}
displayAirplane();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create an empty object and then assign a property with a value when you export it.

A
const Airplane = {};
module.exports = {
myAirplane: 'CloudJet',
displayAirplane ()function {
return this.myAirplane;
}
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How could you export a named export? So modify the following lines of code.

let specialty = '';
function isVegetarian() {
}; 
function isLowSodium() {
};
A
export let specialty = '';
export function isVegetarian() {
}; 
export function isLowSodium() {
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When exporting a module, you can export it as a different name, export the following:

let specialty = '';
function isVegetarian() {
}; 
function isLowSodium() {
};

as different names

A
let specialty = '';
function isVegetarian() {
}; 
function isLowSodium() {
};

export {speciality as specials, isVegetarian as isVeg, isLowSodium as isLowSod}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly