doc: modernize and fix code examples in modules.md
* Replace `var` by `const`. * Fix semicolons. * Add missing code marks. * Unify quotes. * Comment out ellipsis. * Use object destructuring. * Use exponentiation operator. * Replace snake_case by camelCase. PR-URL: https://github.com/nodejs/node/pull/12224 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
This commit is contained in:
parent
96619fc97f
commit
166a15669f
@ -20,9 +20,9 @@ directory as `foo.js`.
|
|||||||
Here are the contents of `circle.js`:
|
Here are the contents of `circle.js`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const PI = Math.PI;
|
const { PI } = Math;
|
||||||
|
|
||||||
exports.area = (r) => PI * r * r;
|
exports.area = (r) => PI * r ** 2;
|
||||||
|
|
||||||
exports.circumference = (r) => 2 * PI * r;
|
exports.circumference = (r) => 2 * PI * r;
|
||||||
```
|
```
|
||||||
@ -44,7 +44,7 @@ Below, `bar.js` makes use of the `square` module, which exports a constructor:
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const square = require('./square.js');
|
const square = require('./square.js');
|
||||||
var mySquare = square(2);
|
const mySquare = square(2);
|
||||||
console.log(`The area of my square is ${mySquare.area()}`);
|
console.log(`The area of my square is ${mySquare.area()}`);
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -54,12 +54,12 @@ The `square` module is defined in `square.js`:
|
|||||||
// assigning to exports will not modify module, must use module.exports
|
// assigning to exports will not modify module, must use module.exports
|
||||||
module.exports = (width) => {
|
module.exports = (width) => {
|
||||||
return {
|
return {
|
||||||
area: () => width * width
|
area: () => width ** 2
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
The module system is implemented in the `require("module")` module.
|
The module system is implemented in the `require('module')` module.
|
||||||
|
|
||||||
## Accessing the main module
|
## Accessing the main module
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ To get the exact filename that will be loaded when `require()` is called, use
|
|||||||
the `require.resolve()` function.
|
the `require.resolve()` function.
|
||||||
|
|
||||||
Putting together all of the above, here is the high-level algorithm
|
Putting together all of the above, here is the high-level algorithm
|
||||||
in pseudocode of what require.resolve does:
|
in pseudocode of what `require.resolve()` does:
|
||||||
|
|
||||||
```txt
|
```txt
|
||||||
require(X) from module at path Y
|
require(X) from module at path Y
|
||||||
@ -565,16 +565,16 @@ To illustrate the behavior, imagine this hypothetical implementation of
|
|||||||
`require()`, which is quite similar to what is actually done by `require()`:
|
`require()`, which is quite similar to what is actually done by `require()`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function require(...) {
|
function require(/* ... */) {
|
||||||
var module = { exports: {} };
|
const module = { exports: {} };
|
||||||
((module, exports) => {
|
((module, exports) => {
|
||||||
// Your module code here. In this example, define a function.
|
// Your module code here. In this example, define a function.
|
||||||
function some_func() {};
|
function someFunc() {}
|
||||||
exports = some_func;
|
exports = someFunc;
|
||||||
// At this point, exports is no longer a shortcut to module.exports, and
|
// At this point, exports is no longer a shortcut to module.exports, and
|
||||||
// this module will still export an empty default object.
|
// this module will still export an empty default object.
|
||||||
module.exports = some_func;
|
module.exports = someFunc;
|
||||||
// At this point, the module will now export some_func, instead of the
|
// At this point, the module will now export someFunc, instead of the
|
||||||
// default object.
|
// default object.
|
||||||
})(module, module.exports);
|
})(module, module.exports);
|
||||||
return module.exports;
|
return module.exports;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user