-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
doc: add more detailed illustration for module.exports and exports.
#9552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Line wrap at 80 columns. comma splice. `The module wrapper` to [module wrapper](#modules_the_module_wrapper) parenthesis error.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -481,23 +481,26 @@ added: v0.1.16 | |
|
|
||
| * {Object} | ||
|
|
||
| `module.exports` object is used to export values in a js file. It is | ||
| initialized as `{}` by Module system, and passed to `The module wrapper` as argument(This makes `module.exports` and `exports` available in a js file, | ||
| the details will be described below). `require` return the processed | ||
| `module.exports` as the exports of a module. | ||
| The `module.exports` object is used to export values in a js file. It is | ||
| initialized as `{}` by Module system, and passed to | ||
| [module wrapper](#modules_the_module_wrapper) as argument. (This makes | ||
| `module.exports` and `exports` available in a js file, the details will be | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ", the details..." unnecessary, remove, or replace with a "see LINK" that links to the docs. |
||
| described below.) `require` returns the processed `module.exports` as the | ||
| exports of a module. | ||
|
|
||
| #### exports alias | ||
| <!-- YAML | ||
| added: v0.1.16 | ||
| --> | ||
|
|
||
| There is no magic behind `exports`, it is the `module.exports` passed to | ||
| `The module wrapper` as the first argument. | ||
| There is no magic behind `exports`. It is the `module.exports` passed to | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove "there is no magic" phrase, it adds no value. "It is the" change to " There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The key point of this sentence is i want to remove the alias, because of this word make some one like me very confused , till check the source code. alias tend to refer to something like reference, imply the things happened in export will reflect into module.export. But it is indeed a value copy from module.export by argument. |
||
| [module wrapper](#modules_the_module_wrapper) as the first argument. | ||
|
|
||
| Here is a minimized code logic of `require` to illustrate the export principle, | ||
| and how `module.exports` and `exports` works:<br/> | ||
| Here is a minimized code logic of `require` to illustrate the export | ||
| principle, and how `module.exports` and `exports` works:<br/> | ||
|
|
||
| As described in `The module wrapper`, your js file will be wrapped like this: | ||
| As described in [module wrapper](#modules_the_module_wrapper), your js file | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. js -> Javascript There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid the use of informal pronouns such as 'you', 'your', 'us', etc. |
||
| will be wrapped like this: | ||
| ```js | ||
| (function (exports, require, module, __filename, __dirname) { // fileWrapper | ||
| // Your module code actually lives in here | ||
|
|
@@ -529,10 +532,10 @@ points: | |
| As a result, the export results of a module is a returned value of | ||
| `module.exports` at the tickcount which `require` is returned. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not true There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the behaviors of this. I thought whenever the user do anything in a file 'x.js', the result of require('./x.js') in 'y.js', will always be the returned value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know exactly what you are trying to say, but I believe you are saying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, i want to say it is the value of the returned There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is why i write a pseudo code for require, a pseudo code will always exactly and cross langauges and terms. (But if i am good at English, may be i do not need that pseudo code to explain what it should be.) |
||
|
|
||
| Let's illustrate the behaviors and principles with codes. ( You can use either | ||
| `_require` or directly a js file to check these behaviors. ) | ||
| Let's illustrate the behaviors and principles with codes. (You can use either | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Trott is this colloquial "let's" and "you" used in node documentation? "codes" -> "code" |
||
| `_require` or directly a js file to check these behaviors.) | ||
|
|
||
| Synchronize behaviors( Demonstrate with the minimized `_require` above ): | ||
| Synchronize behaviors (Demonstrate with the minimized `_require` above) : | ||
| ```js | ||
| function synchronize(exports, _, module) { // the module wrapper | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not a useful way to describe how to write modules, since users do not actually write modules like this |
||
| // Your module codes actually lives below: | ||
|
|
@@ -561,7 +564,7 @@ console.log(exported); | |
| */ | ||
| ``` | ||
|
|
||
| Asynchronous behaviors ( directly check in file):<br/> | ||
| Asynchronous behaviors (directly checking in file) :<br/> | ||
| Assume you have a `x.js`: | ||
| ```js | ||
| // Let's name the exported value, to make things clearly. | ||
|
|
@@ -591,7 +594,8 @@ setTimeout( () => { | |
| There is one more thing to notice: `this`. Because `module.exports` was passed | ||
| as `this` argument for `The module wrapper`, you may used `this` as an alias of | ||
| `exports` to export values previously, But since `this` is an undocumented | ||
| value, the behavior of `this` is not guaranteed. Ex: when you use `babel`, `transform-es2015-modules-commonjs` will break `this`, so you should not use | ||
| value, the behavior of `this` is not guaranteed. Ex: when you use `babel`, | ||
| `transform-es2015-modules-commonjs` will break `this`, so you should not use | ||
| `this` as a shortcut for export. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't agree with the above. this is documented, its documented right here! And if the module does not work when some random thirdparty tool is used to convert it to some other form... that should be part of that tools documentation, not proposed as a general rule for node modules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did do find this in old module docs. searched in doc/api/module.md again previously. |
||
|
|
||
| **As a guideline**, never use 'this' to export, and if the `exports` and | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"as an argument". Remove the parentheses around the complete sentence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: replace:
with: