ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • module.exports II
    Study/JavaScript 2020. 5. 14. 17:01

    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.

     


    file = 2-airplane.js

     

    module.exports = {
      myAirplane : "CloudJet",
      displayAirplane: function() {
        return this.myAirplane;
      }
    };

     

    file = 2-missionControl.js

     

    const Airplane = require('./2-airplane.js');
    
    console.log(Airplane.displayAirplane());

    'Study > JavaScript' 카테고리의 다른 글

    import  (0) 2020.05.14
    export default  (0) 2020.05.14
    require()  (0) 2020.05.14
    module.exports  (0) 2020.05.14
    Hello Modules  (0) 2020.05.14

    댓글

Designed by Tistory.