| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 06e8120 commit 18c3881
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -529,34 +529,51 @@ const x = require('./x'); | |||
| 529 | 529 | console.log(x.a); | |
| 530 | 530 | ``` | |
| 531 | 531 | ||
| 532 | - #### exports alias | ||
| 532 | + #### exports shortcut | ||
| 533 | 533 | <!-- YAML | |
| 534 | 534 | added: v0.1.16 | |
| 535 | 535 | --> | |
| 536 | 536 | ||
| 537 | - The `exports` variable that is available within a module starts as a reference | ||
| 538 | - to `module.exports`. As with any variable, if you assign a new value to it, it | ||
| 539 | - is no longer bound to the previous value. | ||
| 537 | + The `exports` variable is available within a module's file-level scope, and is | ||
| 538 | + assigned the value of `module.exports` before the module is evaluated. | ||
| 539 | + | ||
| 540 | + It allows a shortcut, so that `module.exports.f = ...` can be written more | ||
| 541 | + succinctly as `exports.f = ...`. However, be aware that like any variable, if a | ||
| 542 | + new value is assigned to `exports`, it is no longer bound to `module.exports`: | ||
| 543 | + | ||
| 544 | + ```js | ||
| 545 | + module.exports.hello = true; // Exported from require of module | ||
| 546 | + exports = { hello: false }; // Not exported, only available in the module | ||
| 547 | + ``` | ||
| 548 | + | ||
| 549 | + When the `module.exports` property is being completely replaced by a new | ||
| 550 | + object, it is common to also reassign `exports`, for example: | ||
| 551 | + | ||
| 552 | + ```js | ||
| 553 | + module.exports = exports = function Constructor() { | ||
| 554 | + // ... etc. | ||
| 555 | + ``` | ||
| 540 | 556 | ||
| 541 | 557 | To illustrate the behavior, imagine this hypothetical implementation of | |
| 542 | - `require()`: | ||
| 558 | + `require()`, which is quite similar to what is actually done by `require()`: | ||
| 543 | 559 | ||
| 544 | 560 | ```js | |
| 545 | 561 | function require(...) { | |
| 546 | - // ... | ||
| 562 | + var module = { exports: {} }; | ||
| 547 | 563 | ((module, exports) => { | |
| 548 | - // Your module code here | ||
| 549 | - exports = some_func; // re-assigns exports, exports is no longer | ||
| 550 | - // a shortcut, and nothing is exported. | ||
| 551 | - module.exports = some_func; // makes your module export 0 | ||
| 564 | + // Your module code here. In this example, define a function. | ||
| 565 | + function some_func() {}; | ||
| 566 | + exports = some_func; | ||
| 567 | + // At this point, exports is no longer a shortcut to module.exports, and | ||
| 568 | + // this module will still export an empty default object. | ||
| 569 | + module.exports = some_func; | ||
| 570 | + // At this point, the module will now export some_func, instead of the | ||
| 571 | + // default object. | ||
| 552 | 572 | })(module, module.exports); | |
| 553 | - return module; | ||
| 573 | + return module.exports; | ||
| 554 | 574 | } | |
| 555 | 575 | ``` | |
| 556 | 576 | ||
| 557 | - As a guideline, if the relationship between `exports` and `module.exports` | ||
| 558 | - seems like magic to you, ignore `exports` and only use `module.exports`. | ||
| 559 | - | ||
| 560 | 577 | ### module.filename | |
| 561 | 578 | <!-- YAML | |
| 562 | 579 | added: v0.1.16 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments