Javascript modules

I’m working on modules and i’m confused by two things. First thing is

let myAirplane = {};
module.exports = {
myAirplane: “CloudJet”,
displayAirplane: function() {
return this.myAirplane;
Why do you i need to make the empty object let myAirplane?

the 2nd is

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

If i made my varible something random, and then tried to used dot notation on my Airplane object it would give me an error. But i’m not using dot notation to call something from my variable, i’m using it to call something from my object in another file. Why does the const variable name need to be equal to what i’m calling in my console.log? why can’t my variable be air and still have console.log(Airplane.displayAirplane()); work? Sorry if my question is confusing.

Please post a link to the project (copy the URL of the webpage).

When the module is exported, the variable myAirplane will have the scope of where the module is exported from. If we write let inside a block, { let ... } the variable is scoped only to the block, and not the rest of the program (or the current scope where it is declared). By defining it outside of the block, we have access to the variable both outside and inside the block.

Airplane is the name we’re giving the imported object. We cannot access attributes or methods of that object by any other name. Now if we called it Air then we would log it as Air.method().

how can i tell that const Airplane = require(’./1-airplane.js’); is an object? I thought it was just a varible with a function stored in it

I’m also after a solution about this, any one please?

Because you declared it as an object, with a displayAirplane() method built in.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.