Modules: why is this variable not added to the object or exported?


#1

I don’t know where else to post this. I didn’t see a board specifically for modules.

I don’t understand why the variable payGrades doesn’t need to be exported, especially since it is called in a function that is exported. How can I tell what does and does not need to be exported? Is there anything wrong with simply exporting everything as a matter of course?

let Employee = {
  salary: 100000,
};

let payGrades = {
  entryLevel: { taxMultiplier: .05, benefits: ['health'], minSalary: 10000, maxSalary: 49999 },
  midLevel: { taxMultiplier: .1, benefits: ['health', 'housing'], minSalary: 50000, maxSalary: 99999 },
  seniorLevel: { taxMultiplier: .2, benefits: ['health', 'housing', 'wellness', 'gym'], minSalary: 100000, maxSalary: 200000 }
};

function getCadre() {
  if (Employee.salary >= payGrades.entryLevel.minSalary && Employee.salary <= payGrades.entryLevel.maxSalary) {
    return 'entryLevel';
  } else if (...) {...} else ...;
}

function calculateTax() {...}

function getBenefits() {...}

function calculateBonus() {...}

function reimbursementEligibility() {...}

export { Employee, getCadre as cadre, calculateTax as tax, getBenefits as benefits, calculateBonus as bonus, reimbursementEligibility as reimbursement };

#2

@oduffy. another vet at the door.


#3

Hi, mtf. I’m not sure I understand your response.


#4

Ignore my comment. How much of the JavaScript track have you completed?


#5

I’m on the first project for the requests section of “intermediate JavaScript” section of the “web development” path. I believe it’s the last section of the “Introduction to JavaScript” course. I plan to go through all other JS related courses (and probably take an intensive if I can scrape together the money) once I’m done with the web development path.

While I’ve got your attention, I am also confused about another thing. Right after learning to implement modules, files called “helpers.js” and “helperJavaScript.js” started showing up. These were called with script tags in the HTML, but no code was imported or exported. The question I have is: if I can use code from another JS file without importing it, why bother importing at all?

Thanks for your continued assistance.


#6

Post a link to the project so we can look into it further. I don’t know a lot about modules other than they are very useful and re-usable by other programs. Write once, use many times. Instead of copying code from one program to use in another write the code once in a module and let any program import it.

This is an advanced topic so don’t feel as though you need to master this to move on. Focus on mastering the fundamentals and somewhere down the road you will reach for this concept in your day to day work. Modules and packages are as ubiquitous as snow in today’s development world. You’ll soon be immersed in them.

For now, it is well to understand functions and objects, scope and context, bindings and dependencies, control flow and event handling, to name a few of the general concepts. Somebody more deeply rooted in the advanced side of JS will need to weigh in on your question. Likely I’ve done enough damage, already.


#7

Okay, I will try. Here’s a project that illustrates both issues: https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-requests/projects/wanderlust

I don’t think you’ve done any damage. There don’t seem to be many people willing to help on this board, so your advice has been very much appreciated. Thank you for all of your help. I guess I will have to experiment and ask on Stack Exchange or GitHub. I feel rather baffled by AJAX, to be honest, so I’ll have to go through that whole section a few more times, anyway.


#8

To be fair, AJAX is no simple matter for anybody to learn. It might be a good idea to dig into the archives for the early white papers on XHR (XMLHttpRequest) and Wikipedia for the same topic. It was first developed by Microsoft and took quite awhile to make its way into the mainstream. AJAX is not much more than a new name on a little understand (at the time) object. Asynchronous JavaScript And XML. Find the Wikipedia article on the subject.

Today, without a broad understanding of XHR and AJAX techniques, the current implementation of FETCH and PROMISE will be tough to learn, especially as we are being shown a variety of tetchniques all at once. How do we choose a viable technique for our own projects? (Rhetorical question.)

Bottom line, don’t try to squeeze all this in with other tricky concepts. It (and modules) both deserve a large degree of our attention and time. As mentioned earlier, don’t be afraid to put these on the back burner while you bone up on the fundamentals and get lots and lots of practice and review with those. We don’t want to be stumbling over code concepts while learning either of the above.

Also, be sure your understanding of objects is rock solid, then drill into classes and master that. Modules are a natural progression from classes.


#9

I will do that, thank you. It’s time I ventured away from Codecademy and started really digging into this stuff.

I think what perplexes me most about the Promise system is the timing aspect. I went as far as to make an ASCII copy of a diagram from the MDN documentation for my extensive notes (this one: https://mdn.mozillademos.org/files/15911/promises.png ).

So, if I understand this properly, the Promise is only ever in one of three possible states: fullfilled (executes the .then functions), rejected (executes the rejection functions and catches an error throw), or waiting (and I think the use of “await” is necessary specifically to describe what this waiting state should look like?). What I really don’t understand is this: does the fetch request create the Promise, or does the promise constantly exist like a loop checking for player input in a video game?

I don’t know, it’s all very confusing, especially because I was introduced to this. through objects and classes referencing their own properties, but this. seems much more broad, and it’s coming up constantly in my research into fetch() and related concepts.

Thankfully, objects and classes already make a lot more sense, to me. The real problem is that I keep confusing iterators, methods, and even parameters, not knowing when a return is inappropriate, and confusing keys chained in object calls for methods and built-in methods. Maybe I need to buy some books on these subjects.


#10

All the more reason to set aside AJAX for the moment and focus on objects and iterators. Books are good to have at hand, but so too is online documentation. Don’t spread yourself too thin.


#11

Thanks, mtf. I’ll review the basics.