Skip to content

Commit 5677694

Browse files
committed
Add ES2019 code samples
1 parent c221209 commit 5677694

13 files changed

+147
-4
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,12 +1466,12 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
14661466
14671467
Whereas, **flatMap()** method combines `map()` and `flat()` into one method. It first creates a new array with the return value of a given function and then concatenates all sub-array elements of the array.
14681468
```js
1469-
const numberArray = [[1], [2], [3], [4], [5]];
1469+
const numberArray1 = [[1], [2], [3], [4], [5]];
14701470

1471-
console.log(numberArray.flatMap(value => [value * 10])); // [10, 20, 30, 40, 50]
1471+
console.log(numberArray1.flatMap(value => [value * 10])); // [10, 20, 30, 40, 50]
14721472
```
14731473
1474-
2. ### Object formEntries
1474+
2. ### Object fromEntries
14751475
14761476
In JavaScript, it is very commonn to transforming data from one format. ES2017 introduced `Object.entries()` method to objects into arrays.
14771477
@@ -1541,7 +1541,6 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
15411541
console.log(Symbol().description); // unefined
15421542

15431543
console.log(Symbol.iterator.description); // "Symbol.iterator"
1544-
15451544
```
15461545
15471546
5. ### Optional catch binding

es2015/13.set.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let mySet = new Set()
2+
3+
mySet.add(1);
4+
mySet.add(2);
5+
mySet.add(2);
6+
mySet.add('some text here');
7+
mySet.add({one: 1, two: 2 , three: 3});
8+
console.log(mySet); // Set [ 1, 2, 'some text here', {one: 1, two: 2 , three: 3} ]
9+
console.log(mySet.size) // 4
10+
console.log(mySet.has(2)); // true

es2015/14.weakset.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let myUserSet = new WeakSet();
2+
3+
let john = { name: "John" };
4+
let rocky = { name: "Rocky" };
5+
let alex = { name: "Alex" };
6+
let nick = { name: "Nick" };
7+
8+
myUserSet.add(john);
9+
myUserSet.add(rocky);
10+
myUserSet.add(john);
11+
myUserSet.add(nick);
12+
13+
console.log(myUserSet.has(john)); // true
14+
console.log(myUserSet.has(alex)); // false
15+
console.log(myUserSet.delete(nick));
16+
console.log(myUserSet.has(nick)); // false
17+
18+
john = null;

es2015/15.map.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
let typeMap = new Map();
2+
3+
var keyObj = {'one': 1}
4+
5+
typeMap.set('10', 'string'); // a string key
6+
typeMap.set(10, 'number'); // a numeric key
7+
typeMap.set(true, 'boolean'); // a boolean key
8+
typeMap.set(keyObj, 'object'); // an object key
9+
10+
11+
console.log(typeMap.get(10) ); // number
12+
console.log(typeMap.get('10') ); // string
13+
console.log(typeMap.get(keyObj)) // object
14+
console.log(typeMap.get({'one': 1})) // undefined
15+
16+
console.log(typeMap.size ); // 3
17+
18+
for(let item of typeMap) {
19+
console.log(item);
20+
}
21+
22+
23+
for(let item in typeMap) {
24+
console.log(item);
25+
}

es2015/16.weakmap.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var weakMap = new WeakMap();
2+
3+
var obj1 = {}
4+
var obj2 = {}
5+
6+
7+
weakMap.set(obj1, 1);
8+
weakMap.set(obj2, 2);
9+
weakMap.set({}, {"four": 4});
10+
11+
console.log(weakMap.get(obj2)); // 2
12+
console.log(weakMap.has({})); // return false even though empty object exists as key. Because the keys have different references
13+
14+
delete obj2;
15+
console.log(weakMap.get(obj2)); // 2
16+
weakMap.delete(obj1)
17+
console.log(weakMap.get(obj1)); //undefined

es2019/1.array-flat-flatmap.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// flat
2+
const numberArray = [[1, 2], [[3], 4], [5, 6]];
3+
const charArray = ['a', , 'b', , , ['c', 'd'], 'e'];
4+
const flattenedArrOneLevel = numberArray.flat(1);
5+
const flattenedArrTwoLevel = numberArray.flat(2);
6+
const flattenedCharArrOneLevel = charArray.flat(1);
7+
8+
console.log(flattenedArrOneLevel); // [1, 2, [3], 4, 5, 6]
9+
console.log(flattenedArrTwoLevel); // [1, 2, 3, 4, 5, 6]
10+
console.log(flattenedCharArrOneLevel); // ['a', 'b', 'c', 'd', 'e']
11+
12+
// flatMap
13+
const numberArray1 = [[1], [2], [3], [4], [5]];
14+
15+
console.log(numberArray1.flatMap(value => [value * 10])); // [10, 20, 30, 40, 50]

es2019/2.object-fromentries.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Iterable to objects
2+
const arr = [ ['a', '1'], ['b', '2'], ['c', '3'] ];
3+
const obj = Object.fromEntries(arr);
4+
console.log(obj); // { a: "1", b: "2", c: "3" }
5+
6+
// URLParams
7+
const paramsString = 'param1=foo&param2=baz';
8+
const searchParams = new URLSearchParams(paramsString);
9+
10+
Object.fromEntries(searchParams); // => {param1: "foo", param2: "baz"}

es2019/3.trimstart-trimend.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let messageTwo = " Hello World!! ";
2+
console.log(messageTwo.trimStart()); //Hello World!!
3+
console.log(messageTwo.trimEnd()); // Hello World!!

es2019/4.symbol-description.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
console.log(Symbol('one').description); // one
2+
3+
console.log(Symbol.for('one').description); // "one"
4+
5+
console.log(Symbol('').description); // ''
6+
7+
console.log(Symbol().description); // unefined
8+
9+
console.log(Symbol.iterator.description); // "Symbol.iterator"

es2019/5.optional-catch-binding.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let isTheFeatureImplemented = false;
2+
try {
3+
if(isFeatureSupported()) {
4+
isTheFeatureImplemented = true;
5+
}
6+
} catch (unused) {}

es2019/6.json-improvements.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// JSON Superset
2+
console.log(JSON.parse('"\u2028"')); // ''
3+
4+
// Well Formed JSON.Stringify
5+
console.log(JSON.stringify("\uD800")); // '"\ud800"'

es2019/7.array-stable-sort.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const users = [
2+
{ name: "Albert", age: 30 },
3+
{ name: "Bravo", age: 30 },
4+
{ name: "Colin", age: 30 },
5+
{ name: "Rock", age: 50 },
6+
{ name: "Sunny", age: 50 },
7+
{ name: "Talor", age: 50 },
8+
{ name: "John", age: 25 },
9+
{ name: "Kindo", age: 25 },
10+
{ name: "Lary", age: 25 },
11+
{ name: "Minjie", age: 25 },
12+
{ name: "Nova", age: 25 }
13+
]
14+
users.sort((a, b) => a.age - b.age);

es2019/8.function-tostring.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function sayHello(message) {
2+
let msg = message;
3+
//Print message
4+
console.log(`Hello, ${msg}`);
5+
}
6+
7+
console.log(sayHello.toString());
8+
// function sayHello(message) {
9+
// let msg = message;
10+
// //Print message
11+
// console.log(`Hello, ${msg}`);
12+
// }

0 commit comments

Comments
 (0)