Skip to content

Commit 7c46dfa

Browse files
committed
Integrate tech editing for Chapter 10
1 parent 1c56ce0 commit 7c46dfa

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

10_modules.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ dirty.
4646
{{index dependency}}
4747

4848
_Modules_ are an attempt to avoid these problems. A ((module)) is a
49-
piece of program that specifies which other pieces it relies on (its
50-
_dependencies_) and which functionality it provides for other modules
49+
piece of program that specifies which other pieces it relies on
50+
and which functionality it provides for other modules
5151
to use (its _((interface))_).
5252

5353
{{index "big ball of mud"}}
@@ -175,7 +175,7 @@ people's packages, make sure you are aware of their license.
175175
## Improvised modules
176176

177177
Until 2015, the JavaScript language had no built-in module system.
178-
People had been building large systems in JavaScript for over a decade
178+
Yet people had been building large systems in JavaScript for over a decade
179179
though, and they _needed_ ((module))s.
180180

181181
{{index [function, scope]}}
@@ -243,14 +243,17 @@ function evalAndReturnX(code) {
243243
244244
console.log(evalAndReturnX("var x = 2"));
245245
// → 2
246+
console.log(x);
247+
// → 1
246248
```
247249

248250
{{index "Function constructor"}}
249251

250252
A less scary way of interpreting data as code is to use the `Function`
251253
constructor. It takes two arguments: a string containing a
252254
comma-separated list of argument names and a string containing the
253-
function body.
255+
function body. It wraps the code in a function value so that it gets
256+
its own scope and won't do odd things with other scopes.
254257

255258
```
256259
let plusOne = Function("n", "return n + 1;");
@@ -317,7 +320,7 @@ exports.formatDate = function(date, format) {
317320
{{index "destructuring binding"}}
318321

319322
The interface of `ordinal` is a single function, whereas `date-names`
320-
exports an object containing multiple things—the two values we use are
323+
exports an object containing multiple things—`days` and `months` are
321324
arrays of names. Destructuring is very convenient when creating
322325
bindings for imported interfaces.
323326

@@ -478,6 +481,11 @@ console.log(dayNames.length);
478481
// → 7
479482
```
480483

484+
Another important difference is that ES module imports happen before
485+
a module's script starts running. That means `import` declarations
486+
may not appear inside functions or blocks, and the names of
487+
dependencies can only be quoted strings, not arbitrary expressions.
488+
481489
At the time of writing, the JavaScript community is in the process of
482490
adopting this module style. But it has been a slow process. It took
483491
a few years, after the format was specified, for browsers and Node.js
@@ -629,7 +637,9 @@ uses a graph format similar to ours, but instead of arrays, it uses
629637
objects whose property values are numbers—the weights of the edges.
630638

631639
So if we wanted to use that package, we'd have to make sure that our
632-
graph was stored in the format it expects.
640+
graph was stored in the format it expects. All edges get the same
641+
weight, since our simplified model treats each road as having the
642+
same cost (one turn).
633643

634644
```
635645
const {find_path} = require("dijkstrajs");

0 commit comments

Comments
 (0)