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
Copy file name to clipboardExpand all lines: README.md
+27-27Lines changed: 27 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -130,7 +130,7 @@ Person.prototype.prefixName = function (arr) {
130
130
returnarr.map(function (character) {
131
131
returnthis.name+ character;
132
132
}, this);
133
-
}
133
+
};
134
134
```
135
135
136
136
As well as bind the context:
@@ -144,7 +144,7 @@ Person.prototype.prefixName = function (arr) {
144
144
returnarr.map(function (character) {
145
145
returnthis.name+ character;
146
146
}.bind(this));
147
-
}
147
+
};
148
148
```
149
149
150
150
@@ -157,7 +157,7 @@ function Person(name) {
157
157
158
158
Person.prototype.prefixName=function (arr) {
159
159
returnarr.map(character=>this.name+ character);
160
-
}
160
+
};
161
161
```
162
162
163
163
> **Best Practice**: Use **Arrow Functions** whenever you need to preserve the lexical value of **this**.
@@ -221,11 +221,11 @@ In ES6, we now have access to a terser implementation:
221
221
Using **Template Literals**, we can now construct strings that have special characters in them without needing to escape them explicitly.
222
222
223
223
```javascript
224
-
var text ="This string contains \"double quotes\" which are escaped."
224
+
var text ="This string contains \"double quotes\" which are escaped.";
225
225
```
226
226
227
227
```javascript
228
-
let text =`This string contains "double quotes" which are escaped.`
228
+
let text =`This string contains "double quotes" which are escaped.`;
229
229
```
230
230
231
231
**Template Literals** also support interpolation, which makes the task of concatenating strings and values:
@@ -251,7 +251,7 @@ var text = (
251
251
'cat\n'+
252
252
'dog\n'+
253
253
'nickelodeon'
254
-
)
254
+
);
255
255
```
256
256
257
257
Or:
@@ -261,7 +261,7 @@ var text = [
261
261
'cat',
262
262
'dog',
263
263
'nickelodeon'
264
-
].join('\n')
264
+
].join('\n');
265
265
```
266
266
267
267
**Template Literals** will preserve new lines for us without having to explicitly place them in:
@@ -270,15 +270,15 @@ var text = [
270
270
let text = ( `cat
271
271
dog
272
272
nickelodeon`
273
-
)
273
+
);
274
274
```
275
275
276
276
277
277
**Template Literals** can accept expressions, as well:
278
278
279
279
```javascript
280
-
let today =newDate()
281
-
let text =`The time and date is ${today.toLocaleString()}`
280
+
let today =newDate();
281
+
let text =`The time and date is ${today.toLocaleString()}`;
282
282
```
283
283
284
284
<sup>[(back to table of contents)](#table-of-contents)</sup>
@@ -306,13 +306,13 @@ console.log(b); // 2
306
306
### Destructuring Objects
307
307
308
308
```javascript
309
-
var luke = { occupation:'jedi', father:'anakin' }
309
+
var luke = { occupation:'jedi', father:'anakin' };
310
310
var occupation =luke.occupation; // 'jedi'
311
311
var father =luke.father; // 'anakin'
312
312
```
313
313
314
314
```javascript
315
-
let luke = { occupation:'jedi', father:'anakin' }
315
+
let luke = { occupation:'jedi', father:'anakin' };
316
316
let {occupation, father} = luke;
317
317
console.log(occupation); // 'jedi'
318
318
console.log(father); // 'anakin'
@@ -327,10 +327,10 @@ Prior to ES6, we used libraries such as [Browserify](http://browserify.org/) to
327
327
### Exporting in CommonJS
328
328
329
329
```javascript
330
-
module.exports=1
331
-
module.exports= { foo:'bar' }
332
-
module.exports= ['foo', 'bar']
333
-
module.exports=functionbar () {}
330
+
module.exports=1;
331
+
module.exports= { foo:'bar' };
332
+
module.exports= ['foo', 'bar'];
333
+
module.exports=functionbar () {};
334
334
```
335
335
336
336
### Exporting in ES6
@@ -382,9 +382,9 @@ function sumThree(a, b, c) {
382
382
let api = {
383
383
sumTwo,
384
384
sumThree
385
-
}
385
+
};
386
386
387
-
exportdefaultapi
387
+
exportdefaultapi;
388
388
```
389
389
390
390
> **Best Practices**: Always use the **export default** method at **the end** of the module. It makes it clear what is being exported, and saves time by having to figure out what name a value was exported as. More so, the common practice in CommonJS modules is to export a single value or object. By sticking to this paradigm, we make our code easily readable and allow ourselves to interpolate between CommonJS and ES6 modules.
@@ -394,15 +394,15 @@ export default api
394
394
ES6 provides us with various flavors of importing. We can import an entire file:
395
395
396
396
```javascript
397
-
import`underscore`
397
+
import`underscore`;
398
398
```
399
399
400
400
> It is important to note that simply **importing an entire file will execute all code at the top level of that file**.
401
401
402
402
Similar to Python, we have named imports:
403
403
404
404
```javascript
405
-
import { sumTwo, sumThree } from'math/addition'
405
+
import { sumTwo, sumThree } from'math/addition';
406
406
```
407
407
408
408
We can also rename the named imports:
@@ -411,13 +411,13 @@ We can also rename the named imports:
411
411
import {
412
412
sumTwoasaddTwoNumbers,
413
413
sumThreeassumThreeNumbers
414
-
} from'math/addition'
414
+
} from'math/addition';
415
415
```
416
416
417
417
In addition, we can **import all the things** (also called namespace import):
418
418
419
419
```javascript
420
-
import*asutilfrom'math/addition'
420
+
import*asutilfrom'math/addition';
421
421
```
422
422
423
423
Lastly, we can import a list of values from a module:
@@ -524,7 +524,7 @@ function initializeCanvas(
524
524
We can use the spread operator to pass an array of values to be used as parameters to a function:
525
525
526
526
```javascript
527
-
Math.max(...[-1, 100, 9001, -32]) // 9001
527
+
Math.max(...[-1, 100, 9001, -32]);// 9001
528
528
```
529
529
530
530
<sup>[(back to table of contents)](#table-of-contents)</sup>
ES6 provides much needed syntactic sugar for doing this under the hood. We can create Classes directly:
@@ -609,7 +609,7 @@ const keyTwo = Symbol();
609
609
constobject= {};
610
610
611
611
object[key] ='Such magic.';
612
-
object[keyTwo] ='Much Uniqueness'
612
+
object[keyTwo] ='Much Uniqueness';
613
613
614
614
// Two Symbols will never have the same value
615
615
>> key === keyTwo
@@ -658,7 +658,7 @@ let map = new Map([
658
658
for (let key ofmap.keys()) {
659
659
console.log(typeof key);
660
660
// > string, boolean, number, object, function
661
-
};
661
+
}
662
662
```
663
663
664
664
> **Note**: Using non-primitive values such as functions or objects won't work when testing equality using methods such as `map.get( )`. As such, stick to primitive values such as Strings, Booleans and Numbers.
@@ -792,7 +792,7 @@ var fetchJSON = function(url) {
0 commit comments