Javascript modules


#1

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’);
console.log(Airplane.displayAirplane());

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.


#2

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


#3

https://www.codecademy.com/courses/intermediate-javascript-modules/lessons/modules/exercises/module-exports-ii?action=resume_content_item&course_redirect=introduction-to-javascript

https://www.codecademy.com/courses/intermediate-javascript-modules/lessons/modules/exercises/require?action=resume_content_item&course_redirect=introduction-to-javascript


#4

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().


#5

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


#6

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


#7

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


#8

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