Modules Module #29 Flashcards
How is imported module syntax represented
import package from ‘module-name’
What’s the definition of a JavaScript Module?
A module is a JavaScript file that exports one or more values (objects, functions or variables), using the export keyword.
What gets exported when running export default?
An anonymous function
How can a module be imported into HTML?
Script tag with type= module as below
Note: this module import behaves like a defer script load. See efficiently load JavaScript with defer and async
It’s important to note that any script loaded with type=”module” is loaded in strict mode.
What’s the advantage of using export default on scripts that you plan on importing into HTML?
Once the script is loaded you can name it whatever you like and use it in a different JS script
How can you import a React file?
You can import the default export, and any non-default export by name, like in this common React import:
import React, { Component } from ‘react’
How would you rename a module on import?
import { someModule, anotherModule as newlyNamed} from ‘module-name’
How are modules exported?
export default functionName
example: export default str => str.toUpperCase( )
absolute paths are supported for imports
import toUpperCase from ‘https://flavio-es-modules-example.glitch.me/uppercase.js’
relative paths are also supported
import { toUpperCase } from ‘/uppercase.js’
import { toUpperCase } from ‘../uppercase.js’
Always begin the path with a / or ../ THATS NORMAL
How are multiple functions exported?
Call their names. Example: const a = 1 const b = 2 const c = 3
export { a, b, c }
How does a module import multiple modules
Using the universal selector * as such:
import * from ‘module-name’