You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The recommendation is to always return functions or preferably classes
113
-
as properties of the `module.exports` object, and get them individually by name.
117
+
```js
118
+
import { func1, func2 } from'name'
119
+
```
120
+
121
+
The recommendation is to always export functions or preferably classes
122
+
and get them individually by name.
114
123
115
124
### Prefer static classes to group methods
116
125
@@ -119,26 +128,26 @@ functionality) either below a parent object, or, even better, in
119
128
classes with static members.
120
129
121
130
The main advantage of this scheme is that adding new exports will
122
-
only change the interface incrementally, minimising the risk to
123
-
break backward compatibility.
131
+
only change the interface incrementally, minimising the risk of
132
+
breaking backward compatibility.
124
133
125
134
### Use the spread operator
126
135
127
-
Th spread operator expands an iterable into its member objects. It also works
136
+
The spread operator expands an iterable into its member objects. It also works
128
137
with arrays and objects
129
138
130
139
- Create array copies
131
140
132
141
```js
133
-
constarr1= [1, 23]
142
+
constarr1= [1, 2,3]
134
143
constarr2= [...arr1]
135
144
```
136
145
137
146
- Concatenate arrays
138
147
139
148
```js
140
-
constarr1= [1, 23]
141
-
constarr2= [4, 56]
149
+
constarr1= [1, 2,3]
150
+
constarr2= [4, 5,6]
142
151
constarr3= [...arr1, ...arr2]
143
152
```
144
153
@@ -152,7 +161,8 @@ class Base {
152
161
parent:null,
153
162
...args
154
163
})
155
-
// ...
164
+
// ...
165
+
}
156
166
}
157
167
```
158
168
@@ -170,7 +180,7 @@ iterable.forEach((value) => {
170
180
171
181
Debug note: Visual Studio Code is able to step into the lambda function.
172
182
173
-
For more complex loops, use `for...of`.
183
+
For more complex loops, use `for ... of` (not `in`!).
174
184
175
185
```js
176
186
let iterable = [10, 20, 30]
@@ -266,9 +276,9 @@ Please note that `null` is also an object, and everything created with
266
276
### Use Map to guarantee the order of the values
267
277
268
278
Although maps can be conveniently implemented with regular objects,
269
-
the specs do not guarantee the insert order to be preserved.
279
+
the specifications do not guarantee the insert order to be preserved.
270
280
271
-
If the order is important, or if the object need to store other
281
+
If the order is important, or if the object needs to store other
272
282
properties too, use a `Map`.
273
283
274
284
### Make node exports/imports look like ES6 exports/imports
@@ -561,7 +571,7 @@ Use single quotes for strings except to avoid escaping
561
571
```js
562
572
constok='String contains "double" quotes'
563
573
constalsoOk="String contains 'single' quotes or apostrophe"
564
-
constparamOk=`Back quotes string with ${parameter}`
574
+
constparamOk=`Back-quoted string with ${parameter}`
565
575
```
566
576
567
577
### White spaces
@@ -592,9 +602,9 @@ Use named functions.
592
602
593
603
### Exceptions
594
604
595
-
For the native Node.js callback usage, never to ever ever throw anything. It's worse than useless. Just send the error message back as the first argument to the callback.
605
+
For the native Node.js callback usage, throwing anything is worse than useless. Simply send the error message back as the first argument to the callback.
596
606
597
-
But for the modern ES 6 promise usage, exceptions are fine.
607
+
However, for the modern ES 6 promise usage, exceptions are acceptable.
598
608
599
609
## From [Node.js modules](https://nodejs.org/dist/latest-v6.x/docs/api/modules.html)
600
610
@@ -610,19 +620,19 @@ Modules are cached after the first time they are loaded. This means (among other
610
620
611
621
From this point of view, modules behave like [singletons](https://en.wikipedia.org/wiki/Singleton_pattern); they can also be compared to static classes in C++.
612
622
613
-
Leaving status at the module level can be either a blessing or a curse, depending on the environment used to run the module. In server environments, using module-global variables is like using static variables in a multi-threaded environment, if not handled correctly it may have unexpected results.
623
+
Leaving state at the module level can be either a blessing or a curse, depending on the environment used to run the module. In server environments, using module-global variables is like using static variables in a multi-threaded environment; if not handled correctly, it may have unexpected results.
614
624
615
625
### Exports
616
626
617
-
To make some functions and objects visible outside the module, you can add them as properties to the special `modules.exports` object:
627
+
To make some functions and objects visible outside the module, add them as properties to the special `module.exports` object:
618
628
619
629
```js
620
630
constPI=Math.PI
621
631
module.exports.area= (r) =>PI* r * r
622
632
module.exports.circumference= (r) =>2*PI* r
623
633
```
624
634
625
-
Although you can rewrite the `module.export` to be a single function (such as a constructor), still prefer to add them as properties to the object and refer to them explicitly in the `require()` line:
635
+
Although you can rewrite the `module.exports` to be a single function (such as a constructor), it is still preferable to add them as properties to the object and refer to them explicitly in the `require()` line:
626
636
627
637
```js
628
638
constsquare=require('./square.js').square
@@ -646,24 +656,24 @@ When a file is run directly from Node.js, `require.main` is set to its module. T
646
656
require.main===module
647
657
```
648
658
649
-
### The module wrapper
659
+
### The module wrapper (deprecated by ES6)
650
660
651
661
Before a module's code is executed, Node.js will wrap it with a function wrapper that looks like the following:
652
662
653
663
```js
654
664
module=...// an object for the current module
655
-
module.export= {} // an empty object
656
-
exports=module.export// a reference to the exports; avoid using it
665
+
module.exports= {} // an empty object
666
+
exports=module.exports// a reference to the exports; avoid using it
In each module, the `module` variable is a reference to the object representing the current module. `module`isn't actually a global but rather local to each module.
674
+
In each module, the `module` variable is a reference to the object representing the current module. `module`is not actually a global but rather local to each module.
665
675
666
-
The `module.exports` object is created by the Module system. Sometimes this is not acceptable; many want their module to be an object of their own. To do this, assign the desired export object to `module.exports`.
676
+
The `module.exports` object is created by the Module system. Sometimes this is not acceptable; many developers want their module to be an object of their own. To do this, assign the desired export object to `module.exports`.
667
677
668
678
For convenience, `module.exports` is also accessible via the `exports` module-global. Note that assigning a value to `exports` will simply rebind the local exports variable, which is probably not what you want to do; if the relationship between `exports` and `module.exports` seems like magic to you, ignore `exports` and only use `module.exports`.
0 commit comments