Skip to content

Commit b1bf92a

Browse files
committed
Add ES2019 features
1 parent a0ec1f3 commit b1bf92a

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

README.md

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ greet(); // Hello World!
145145
### Classes
146146
The classes are introduced as syntactic sugar over existing prototype based inheritance and constructor functions. So this feature doesn't bring new object-oriented inheritance model to JavaScript.
147147

148-
### Reflect
149-
Reflection is the ability of a code to inspect and manipulate variables, properties, and methods of objects at runtime. JavaScript already provides Object.keys(), Object.getOwnPropertyDescriptor(), and Array.isArray() methods as classic refection features. In ES6, it has been officially provided through Reflect object. Reflect is a new global object which is used to call methods, construct objects, get and set properties, manipulate and extend properties. It is similar to Math and JSON objects and all the methods of this object are static.
150-
151148
### Set
152149
Set is a built-in object to store collections of unique values of any type.
153150
```js
@@ -322,7 +319,47 @@ console.log(Symbol('foo') === Symbol('foo')); // false
322319
console.log(Symbol.for('foo') === Symbol.for('foo')); // true
323320
```
324321
322+
### Promises
323+
324+
### Reflect
325+
Reflection is the ability of a code to inspect and manipulate variables, properties, and methods of objects at runtime. JavaScript already provides Object.keys(), Object.getOwnPropertyDescriptor(), and Array.isArray() methods as classic refection features. In ES6, it has been officially provided through Reflect object. Reflect is a new global object which is used to call methods, construct objects, get and set properties, manipulate and extend properties. It is similar to Math and JSON objects and all the methods of this object are static.
326+
325327
### Proper Tail Calls
328+
**Proper tail call(PTC)** is a technique where the program or code will not create additional stack frames for a recursion when the function call is a tail call.
329+
330+
For example, the below classic or head recursion of factorial function relies on stack for each step. Each step need to be processed upto `n * factorial(n - 1)`
331+
332+
```js
333+
function factorial(n) {
334+
if (n === 0) {
335+
return 1
336+
}
337+
return n * factorial(n - 1)
338+
}
339+
console.log(factorial(5)); //120
340+
```
341+
342+
But if you use Tail recursion functions, they keep passing all the necessary data it needs down the recursion without relying on the stack.
343+
344+
```js
345+
function factorial(n, acc = 1) {
346+
if (n === 0) {
347+
return acc
348+
}
349+
return factorial(n - 1, n * acc)
350+
}
351+
console.log(factorial(5)); //120
352+
```
353+
354+
The above pattern returns the same output as first one. But the accumulator keeps track of total as an argument without using stack memory on recursive calls.
355+
356+
The browsers which supports PTC do not generate stack overflow instead shows Infinity with below inputs,
357+
```js
358+
console.log(factorial(10));
359+
console.log(factorial(100));
360+
console.log(factorial(1000));
361+
console.log(factorial(10000));
362+
```
326363
327364
## ES2016 Or ES7
328365
@@ -659,13 +696,23 @@ console.log(Symbol.for('foo') === Symbol.for('foo')); // true
659696
660697
### Array flat and flatMap
661698
662-
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.
699+
Prior to ES2019, you need to use `reduce() or concat()` methods to get a flat array.
700+
```js
701+
function flatten(arr) {
702+
const flat = [].concat(...arr);
703+
return flat.some(Array.isArray) ? flatten(flat) : flat;
704+
}
705+
flatten([ [1, 2, 3], ['one', 'two', 'three', [22, 33] ], ['a', 'b', 'c'] ]);
706+
707+
```
708+
709+
In ES2019, the `flat()` method is introduced to 'flattens' the nested arrays into the top-level array. The functionality of this method is similar to Lodash's `_.flattenDepth()` function.
663710
664711
```js
665-
const arr = [[1, 2], [[3], 4], [5, 6];
666-
const flattenedArr = arr.flat(2);
712+
const arr = [[1, 2], [[3], 4], [5, 6];
713+
const flattenedArr = arr.flat(2);
667714

668-
console.log(flattenedArr); // => ["1", "2", "3", "4", "5", "6"]
715+
console.log(flattenedArr); // => ["1", "2", "3", "4", "5", "6"]
669716
```
670717
671718
### Object formEntries
@@ -719,6 +766,22 @@ console.log(Symbol.for('foo') === Symbol.for('foo')); // true
719766
```
720767
721768
### Symbol description
769+
While creating symbols, you also can add a description to it for debugging purposes. But there was no method to access the description directly before ES2019. Considering this, ES2019 introduced a read-only description property to retrieve a string containing the description of the Symbol.
770+
771+
This gives the possibility to access symbol description for different variations of Symbol objects
772+
773+
```js
774+
console.log(Symbol('one').description); // one
775+
776+
console.log(Symbol.for('one').description); // "one"
777+
778+
console.log(Symbol('').description); // ''
779+
780+
console.log(Symbol().description); // unefined
781+
782+
console.log(Symbol.iterator.description); // "Symbol.iterator"
783+
784+
```
722785
723786
### Optional catch binding
724787
Prior to ES9, if you don't need `error` variable and omit the same variable then catch() clause won't be invoked. Also, the linters complain about unused variables. Inorder to avoid this problem, the optional catch binding feature is introduced to make the binding parameter optional in the catch clause. If you want to completely ignore the error or you already know the error but you just want to react to that the this feature is going to be useful.

0 commit comments

Comments
 (0)