INTERMEDIATE JAVASCRIPT MODULES
METHOD 1
module.exports =
let Menu = {};creates the object that represents the moduleMenu. The statement contains an uppercase variable namedMenuwhich is set equal to an object.Menu.specialtyis defined as a property of theMenumodule. We add data to theMenuobject by setting properties on that object and giving the properties a value."Roasted Beet Burger with Mint Sauce";is the value stored in theMenu.specialtyproperty.module.exports = Menu;exports theMenuobject as a module.moduleis a variable that represents the module, andexportsexposes the module as an object.
- property
namewith 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
chefsSpecialandisVegfrom theMenuobject. - Here, note that we have an option to alias an object that was not previously aliased when exported. For example, the
isLowSodiumobject that we exported could be aliased with theaskeyword 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
chefsSpecialandisVeg, these would be available to us. If we did not give an alias toisLowSodium, we would call it as defined on theCartemodule.
- We can also use named exports and default exports together.
What I'll be doing tomorrow?
JAVASCRIPT PROMISES
No comments:
Post a Comment