Learn
Intermediate JavaScript Modules
module.exports II

We can also wrap any collection of data and functions in an object, and export the object using module.exports. In menu.js, we could write:

module.exports = { specialty: "Roasted Beet Burger with Mint Sauce", getSpecialty: function() { return this.specialty; } };

In the above code, notice:

  1. module.exports exposes the current module as an object.
  2. specialty and getSpecialty are properties on the object.

Then in order.js, we write:

const Menu = require('./menu.js'); console.log(Menu.getSpecialty());

Here we can still access the behavior in the Menu module.

Instructions

1.

In 2-airplane.js, set module.exports equal to an empty object.

2.

Within the object, create a key called myAirplane and set it to a value "CloudJet".

3.

Again, within module.exports, create another key displayAirplane and set it to an anonymous function. The function should use the this statement to return myAirplane.

4.

In 2-missionControl.js use the require() function to import the Airplane module.

5.

In 2-missionControl.js log the result of calling .displayAirplane() to the console, noting that it is a method of the Airplane object.

Folder Icon

Take this course for free

Already have an account?