|
46 | 46 | {{index dependency}}
|
47 | 47 |
|
48 | 48 | _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 |
51 | 51 | to use (its _((interface))_).
|
52 | 52 |
|
53 | 53 | {{index "big ball of mud"}}
|
@@ -175,7 +175,7 @@ people's packages, make sure you are aware of their license.
|
175 | 175 | ## Improvised modules
|
176 | 176 |
|
177 | 177 | 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 |
179 | 179 | though, and they _needed_ ((module))s.
|
180 | 180 |
|
181 | 181 | {{index [function, scope]}}
|
@@ -243,14 +243,17 @@ function evalAndReturnX(code) {
|
243 | 243 |
|
244 | 244 | console.log(evalAndReturnX("var x = 2"));
|
245 | 245 | // → 2
|
| 246 | +console.log(x); |
| 247 | +// → 1 |
246 | 248 | ```
|
247 | 249 |
|
248 | 250 | {{index "Function constructor"}}
|
249 | 251 |
|
250 | 252 | A less scary way of interpreting data as code is to use the `Function`
|
251 | 253 | constructor. It takes two arguments: a string containing a
|
252 | 254 | 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. |
254 | 257 |
|
255 | 258 | ```
|
256 | 259 | let plusOne = Function("n", "return n + 1;");
|
@@ -317,7 +320,7 @@ exports.formatDate = function(date, format) {
|
317 | 320 | {{index "destructuring binding"}}
|
318 | 321 |
|
319 | 322 | 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 |
321 | 324 | arrays of names. Destructuring is very convenient when creating
|
322 | 325 | bindings for imported interfaces.
|
323 | 326 |
|
@@ -478,6 +481,11 @@ console.log(dayNames.length);
|
478 | 481 | // → 7
|
479 | 482 | ```
|
480 | 483 |
|
| 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 | + |
481 | 489 | At the time of writing, the JavaScript community is in the process of
|
482 | 490 | adopting this module style. But it has been a slow process. It took
|
483 | 491 | 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
|
629 | 637 | objects whose property values are numbers—the weights of the edges.
|
630 | 638 |
|
631 | 639 | 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). |
633 | 643 |
|
634 | 644 | ```
|
635 | 645 | const {find_path} = require("dijkstrajs");
|
|
0 commit comments