In the exercise about named exports (link at bottom) we export some variables from airplane.js
to missionControl.js
and then use a function inside a forEach() in missionControl.js
to log a statement to the console.
It seems that in airplane.js
there is different scoping of these variables compared the the same variable in missionControl.js
even though they have been imported.
I will post the code below. Could someone please help me understand why in airplane.js
, the exported file, requiredStaff
on its own will access this value within the let flightRequirements
variable. But in missionControl.js
we need to use flightRequirements.requiredSpeedRange
to access requiredSpeedRange
within the same let flightRequirements
variable?
Thank you.
Airplane.js code:
export let flightRequirements = {
requiredStaff: 4,
requiredSpeedRange: 700
};
export function meetsStaffRequirements(availableStaff, requiredStaff) {
if (availableStaff.length >= requiredStaff) {
return true;
} else {
return false;
}
};
missonControl.js code:
function displaySpeedRangeStatus() {
availableAirplanes.forEach(function(element) {console.log(element.name + 'meets speed range requirements: ' + meetsSpeedRangeRequirements(element.maxSpeed, element.minSpeed, flightRequirements.requiredSpeedRange));
});
};
Exercise:
https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-modules/lessons/modules/exercises/import-named-imports
If by "requiredStaff
on its own" you’re referring to requiredStaff
used in the function meetsStaffRequirements()
, then the naming is causing some confusion. The requiredStaff
in there is the name of the function parameter, so it isn’t tied to the requiredStaff
property of the flightRequirements
object.
The function could be re-written as this, and it would function exactly the same:
export function meetsStaffRequirements(staffArray, minimumStaff) {
if (staffArray.length >= minimumStaff) {
return true;
} else {
return false;
}
};
If that isn’t what you were referring to or if I’m misunderstanding the question, please let me know.
Yeah i don’t think I explained properly what I am confused with.
The variable flightRequirements
at the top of the first block of code has two key/value pairs. requiredStaff
and requiredSpeedRange
.
In the function in the first block of code meetsStaffRequirements()
, it accesses the requiredStaff
value by just writing requiredStaff
on its own.
In the second code block function displaySpeedRangeStatus()
however, to access requiredSpeedRange
we need to use flightRequirements.requiredSpeedRange
.
I don’t understand why in one function we can access it with just the name of the key/value pair and in the other we need to use the variable name first to gain access. It’s the same variable yet requires two different types of syntax to get in.
Does that make sense?
(top code block is the original file which is then exported with a named export to the second code block’s file.)
That’s the function that I re-wrote to show that it isn’t accessing the value of requiredStaff
from your flightRequirements
object. Notice I changed the name of the parameter so it was clearer that it wasn’t the requiredStaff
you were thinking of. It was just the parameter name that was the same name as that property, so it was causing confusion. In the code I posted, I renamed it to minimumStaff
and it would have no impact on the return value. It’s only evaluating the values were sent to the function as arguments, that function doesn’t read from the flightRequirements
object at all.