Modules Flashcards

1
Q

What are four reasons we might use modules to contain JavaScript code?

A
  1. Allows us to find, fix, and debug more easily
  2. Allows us to reuse and recycle logic in different areas of the application.
  3. Keep information private and protected from other modules
  4. Avoid polluting the global namespace and potential naming collisions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What the does process.argv[n] method return?

A

process.argv is an array of commands passed into the node environment. process.argv[n] returns the nth argument in the command (Zero-based indexing)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

describe two ways of exporting a function from a module

A
  1. By defining a function, then passing that function definition via module.exports.functionName = functionName
  2. By setting module.exports.functionName to a function defined in the export dialogue. module.exports.functionName = function(name){code}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you import a function from a module after it has been exported?

A

You can use the require() function in the receiving file to import a function from a module. The require() function accepts a file path as a string.

require(‘./modules/moduleName.js’ would import all exported functions from moduleName.js.

You will then assign the functions via const functionName = moduleName.functionName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does module.exports() create?

A

module.exports() creates an object that contains all exported functions from the moduleName.js file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why would we use Object Destructuring to import from a module?

A

Object Destructuring allows us to import only select functions from a module when not all of the exported functions are needed:

const { desiredFunctionName } = require(‘./modules/moduleName.js’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we use object deconstruction to import a function from a module file?

A

We would write:

const { desiredFunctionName } = required(‘filePath/to/module.js’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does module importation/exportation differ between node and browser runtime environment?

A

Node uses module.exports and require(‘filePath) syntax

Browsers use export and import syntax

export {exportA, exportB } and import {exportA, exportB} from ‘filePath’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we export and import module files in a browser based runtime environment?

A

export {moduleExport} or export const moduleName = function

import {moduleExport} from ‘filePathName’

we also need to include type=’module’ to the script import to html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do we need to remember when importing modules into HTML via the script tags?

A

We need to include type=”module” in addition to the filepath for the script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What might we do to avoid naming conflicts when importing a module?

A

We might use the import … as … syntax to avoid naming conflicts

import {greet as greetFrench } from ‘filePathName’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we use the default export to export the entire module file and all contained functions?

A

We first define the module code as an object by:

const resources = { codeBlockHere }

and then export {resources as default }

this will export the entire module file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do we import a module exported as default?

A

We use:

import importedResources from ‘filePathName’

note that this is actually import {resources as importedResources} from ‘filePathName’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is different in the import syntax of a browser runtime environment when we are importing a default export?

A

The curly braces are gone from around the imported moduleName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do we create individual function variables having used a default export?

A

We import the default as normal and then deconstruct the default like so:

const {functionOne, functionTwo } = defaultName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can we export functions to avoid naming conflicts?

A

We can use the export … as … syntax, much like the import syntax

export { functionName as newFunctionName } and then

import { newFunctionname } from ‘./filePathName’

17
Q

If we had a large number of functions to export from a module, how might we import them efficiently?

A

We could import all functions form a module in a module object:

import * as Module from ‘filePathname’

We could then call the functions by using dot-notation with the function name - Module.functionName( )