Modules Flashcards

1
Q

What is the module specification used by Node.js initially?

A

CommonJS

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

Why don’t browsers support CommonJS?

A

CommonJS loads modules synchronously.

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

When you use CommonJS, what bindings are available so it can interact with other modules? What do they do?

A

require: a function that makes sure a module is loaded and returns its interface.

exports: an object which is the interface object for the module. It starts out empty and you add properties to it to defined exported values.

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

Can require only be used at the top-level, or can it be used in functions too?

A

Since require is a normal function it can be used anywhere.

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

How do you declare your dependencies with CommonJS?

A

Call require like this:

const ordinal = require("ordinal");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do declare your interface in CommonJS?

A

Use exports or module.exports.

module.exports = { foo: "bar" };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s are two differences between a regular script and a ES module?

A

Regular scripts have bindings defined in the global scope and have no way to directly reference other scripts.

Modules get their own separate scope and support the import/export keywords to declare their dependencies and interface.

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

How to define an interface with ES modules?

A

The export keyword can be put in front of a function, class, or binding definition.

export function doWork() { 
    return 7; 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In ES Modules, how do you make the bindings from another module available in the current module?

A

The import keyword

import { dayName } from "./dayname.js";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the default keyword used for in ES Modules?

A

Often used for modules that export a single binding.

export default ["Winter", "Spring", "Summer", "Autumn"];

This binding can be imported without the braces around the variable name.

import seasonNames from "./seasonname.js";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you import all bindings from a module at the same time using ES Modules?

A

Use import *. You provide a name, and that name will be bound to an object holding all the module’s exports.

import * as dayName from "./dayname.js";
console.log(dayName.dayName(3));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly