Skip to content

Commit ce7a553

Browse files
committed
Add es2018 features
1 parent 8dbd21d commit ce7a553

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

README.md

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
6363
|7 | [Trailing commas](#trailing-commas) |
6464
| | **ES2018 Or ES9**|
6565
|1 | [Async iterators](#async-iterators) |
66-
|2 | [Object rest properties](#object-rest-properties) |
67-
|3 | [Object spread properties](#object-spread-properties) |
68-
|4 | [Promise finally](#promise-finally) |
66+
|2 | [Object rest and spread operators](#object-rest-and-spread-operators) |
67+
|3 | [Promise finally](#promise-finally) |
6968
| | **ES2019 Or ES10**|
7069
|1 | [Array flat and flatMap](#array-flat-and-flatmap) |
7170
|2 | [Object formEntries](#object-formentries) |
@@ -393,6 +392,77 @@ Reflection is the ability of a code to inspect and manipulate variables, propert
393392
394393
## ES2018 Or ES9
395394
395+
### Object rest and spread operators
396+
ES2015 or ES6 introduced both rest parameters and spread operators to convert arguments to array and vice versa using three-dot(...) notation.
397+
1. Rest parameters can be used to convert function arguments to an array
398+
399+
```js
400+
function myfunc(p1, p2, ...p3) {
401+
console.log(p1, p2, p3); // 1, 2, [3, 4, 5, 6]
402+
}
403+
myfunc(1, 2, 3, 4, 5, 6);
404+
```
405+
406+
2. The spread operator works in the opposite way by converting an array into separate arguments in order to pass to a function
407+
408+
```js
409+
const myArray = [10, 5, 25, -100, 200, -200];
410+
console.log( Math.max(...myArray) ); // 200
411+
```
412+
413+
ES2018 enables this rest/spread behavior for objects as well.
414+
415+
1. You can pass object to a function
416+
417+
```js
418+
function myfunc({ a, ...x }) {
419+
console.log(a, x); // 1, { b: 2, c: 3, d:4 }
420+
}
421+
myfunc({
422+
a: 1,
423+
b: 2,
424+
c: 3,
425+
d: 4
426+
});
427+
```
428+
429+
2. The spread operator can be used within other objects
430+
431+
```js
432+
const myObject = { a: 1, b: 2, c: 3, d:4 };
433+
const myNewObject = { ...myObject, e: 5 }; // { a: 1, b: 2, c: 3, d: 4, e: 5 }
434+
```
435+
436+
### Promise finally
437+
Sometimes you may need to avoid duplicate code in the then() and catch() methods.
438+
439+
```js
440+
myPromise
441+
.then(result => {
442+
// process the result and then clean up the resources
443+
})
444+
.catch(error => {
445+
// handle the error and then clean up the resources
446+
});
447+
```
448+
449+
The `finally()` method is useful if you want to do some processing or resource cleanup once the promise is settled(i.e either fulfilled or rejected).
450+
451+
Let's take a below example to hide the loading spinner after the data is fetched and processed.
452+
453+
```js
454+
let isLoading = true;
455+
fetch('http://somesite.com/users')
456+
.then(data => data.json())
457+
.catch(err => console.error(err))
458+
.finally(() => {
459+
isLoading = false;
460+
console.log('Finished loading!!');
461+
})
462+
```
463+
464+
## ES2019 Or ES10
465+
396466
### Array flat and flatMap
397467
398468
The `flat()` method is used to 'flattens' the nested arrays into the top-level array. The functionality of this method is similar to Lodash's `_.flattenDepth()` function.
@@ -454,10 +524,6 @@ Reflection is the ability of a code to inspect and manipulate variables, propert
454524
console.log(messageTwo.trimEnd()); // Hello World!!
455525
```
456526
457-
### Promise finally
458-
459-
### Dynamic import
460-
461527
### Symbol description
462528
463529
### Optional catch binding
@@ -504,7 +570,7 @@ Reflection is the ability of a code to inspect and manipulate variables, propert
504570
505571
**Note:** As shown in the above code, If you still try to access the variable directly from the object then you will receive syntax error.
506572
507-
## ES2019 Or ES10
573+
## ES2020 Or ES11
508574
509575
ES2020 is the current newer version of ECMAScript corresponding to the year 2020. This is the eleventh edition of the ECMAScript Language Specification. Even though this release doesn't bring as many features as ES6, it included some really useful features.
510576

0 commit comments

Comments
 (0)