INTERMEDIATE JAVASCRIPT MODULES
METHOD 1
module.exports =
let Menu = {};
creates the object that represents the moduleMenu
. The statement contains an uppercase variable namedMenu
which is set equal to an object.Menu.specialty
is defined as a property of theMenu
module. We add data to theMenu
object by setting properties on that object and giving the properties a value."Roasted Beet Burger with Mint Sauce";
is the value stored in theMenu.specialty
property.module.exports = Menu;
exports theMenu
object as a module.module
is a variable that represents the module, andexports
exposes the module as an object.
- property
name
with a value'AeroJet'
name: 'AeroJet' USE ":"
EX:
function displaySpeedRangeStatus() {
clear availableAirplanes.forEach(function(element) {
console.log(element.name + ' meets speed range requirements:' + meetsSpeedRangeRequirements(element.maxSpeed, element.minSpeed, flightRequirements.requiredSpeedRange));
});
}
displaySpeedRangeStatus();
Prints:
AeroJet meets speed range requirements:true
SkyJet meets speed range requirements:false
METHOD 2
let availableAirplanes = [
{name: 'AeroJet',
fuelCapacity: 800,
availableStaff: ['pilots', 'flightAttendants', 'engineers', 'medicalAssistance', 'sensorOperators'],
maxSpeed: 1200,
minSpeed: 300
},
{name: 'SkyJet',
fuelCapacity: 500,
availableStaff: ['pilots', 'flightAttendants'],
maxSpeed: 800,
minSpeed: 200
}
];
export { availableAirplanes as aircrafts, flightRequirements as flightReqs, meetsStaffRequirements as meetsStaffReqs, meetsSpeedRangeRequirements as meetsSpeedRangeReqs };
In this code, we got rid of the export key word in front of all the variables and functions. And what replaced that was the last part of the code above. Highlighted, with the keyword 'AS'.
- Same thing with the import as well
WAS: import { chefsSpecial, isVeg } from './menu';
- We import
chefsSpecial
andisVeg
from theMenu
object. - Here, note that we have an option to alias an object that was not previously aliased when exported. For example, the
isLowSodium
object that we exported could be aliased with theas
keyword when imported:import {isLowSodium as saltFree} from 'Menu';
NOW: import * as Carte from './menu';
clear/div>
- This allows us to import an entire module from menu.js as an alias
Carte
. - In this example, whatever name we exported would be available to us as properties of that module. For example, if we exported the aliases
chefsSpecial
andisVeg
, these would be available to us. If we did not give an alias toisLowSodium
, we would call it as defined on theCarte
module.
- We can also use named exports and default exports together.
What I'll be doing tomorrow?
JAVASCRIPT PROMISES
No comments:
Post a Comment