Wednesday, May 8, 2019

JavaScript(14)---INTERMEDIATE JAVASCRIPT MODULES

INTERMEDIATE JAVASCRIPT MODULES

METHOD 1
module.exports =

  1. let Menu = {}; creates the object that represents the module Menu. The statement contains an uppercase variable named Menu which is set equal to an object.
  2. Menu.specialty is defined as a property of the Menu module. We add data to the Menu object by setting properties on that object and giving the properties a value.
  3. "Roasted Beet Burger with Mint Sauce"; is the value stored in the Menu.specialty property.
  4. module.exports = Menu; exports the Menu object as a module. module is a variable that represents the module, and exportsexposes 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';

  1. We import chefsSpecial and isVeg from the Menu object.
  2. 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 the as keyword when imported: import {isLowSodium as saltFree} from 'Menu';

NOW: import * as Carte from './menu';
clear/div>
  1. This allows us to import an entire module from menu.js as an alias Carte.
  2. 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 and isVeg, these would be available to us. If we did not give an alias to isLowSodium, we would call it as defined on the Carte module.
  • We can also use named exports and default exports together.
What I learned today:
What I'll be doing tomorrow?
JAVASCRIPT PROMISES

No comments:

Post a Comment