Skip to content

Commit 7feb349

Browse files
author
Vitalii Telychko
committed
added maps_sets_symbols_callable_entities
1 parent 5bcfc63 commit 7feb349

28 files changed

+866
-91
lines changed

ES6/Callable_entities/avoid_IIFE.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// 1. Replace an IIFE with a block if you wanted to keep a variable local
2+
3+
// // ES5
4+
// (function () {
5+
// var tmp = 'hello';
6+
// }());
7+
//
8+
// console.log(tmp); // Reference Error
9+
10+
// ES6
11+
// {
12+
// let tmp = 'Hello';
13+
// }
14+
//
15+
// console.log(tmp); //Reference Error
16+
17+
// 2. Replace an IIFE with a module
18+
19+
// ES5 Revealing Module Pattern
20+
21+
// var my_module = (function () {
22+
// // Module-private variable:
23+
// var countInvocations = 0;
24+
//
25+
// function myFunc(x) {
26+
// countInvocations++;
27+
//
28+
// }
29+
//
30+
// // Exported by module:
31+
// return {
32+
// myFunc: myFunc
33+
// };
34+
// }());
35+
36+
// This module pattern produces a global variable and is used as follows:
37+
//
38+
// my_module.myFunc(33);
39+
40+
// ES6
41+
// In ECMAScript 6, modules are built in, which is why the barrier to adopting them is low:
42+
//
43+
// // my_module.js
44+
//
45+
// // Module-private variable:
46+
// let countInvocations = 0;
47+
//
48+
// export function myFunc(x) {
49+
// countInvocations++;
50+
//
51+
// }
52+
// This module does not produce a global variable and is used as follows:
53+
//
54+
// import { myFunc } from 'my_module.js';
55+
//
56+
// myFunc(33);
57+
58+
// !!! Method calls: work as with traditional functions, but additionally allow super.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 1. Dynamic dispatch - arr.slice(1)
2+
// 2. Direct call - Array.prototype.call.slice(1)
3+
4+
// 1. Prototype chain
5+
// var arr = ['a', 'b'];
6+
// var p = Object.getPrototypeOf;
7+
//
8+
// console.log(p(arr) === Array.prototype); // true
9+
// console.log(p(p(arr)) === Object.prototype); // true
10+
// p(p(p(arr))); // null
11+
12+
// Properties in “earlier” objects override properties in “later” objects.
13+
// const obj = {a: 1, b: 2};
14+
// console.log(Object.getPrototypeOf(obj));
15+
16+
// 2. Dispatched method calls
17+
// var func = arr.toString(); // 1. retrieve the value of the first property whose name is toString.
18+
// func.call(arr); // 2. Call the value and set the implicit parameter this to the receiver arr
19+
20+
// 3. Direct method calls
21+
// Function.prototype.call(thisValue, arg0?, arg1?, ···)
22+
// Function.prototype.apply(thisValue, argArray)
23+
24+
// const arr = ['a', 'b', 'c'];
25+
// console.log(Object.prototype.toString.call(arr)); // [object Array]
26+
// console.log(Array.prototype.toString.call(arr)); // a, b, c
27+
28+
// Abbreviations for Object.prototype = {} and Array.prototype = []

ES6/Callable_entities/fn_name.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// let func1 = function () {};
2+
// // console.log(func1.name); // func1
3+
4+
// const func = () => {};
5+
// console.log(func.name); // func
6+
7+
// 1. the name of a function is always assigned at creation
8+
// 2. Function names are subject to minification, which means that they will usually change in minified code.
9+
// Depending on what you want to do, you may have to manage function names via strings (which are not minified) or
10+
// you may have to tell your minifier what names not to minify.

ES6/Callable_entities/overview.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
3+
In ES5, a single construct, the (traditional) function, played three roles:
4+
5+
Real (non-method) function
6+
Method
7+
Constructor
8+
9+
In ES6, there is more specialization.
10+
The three duties are now handled as follows.
11+
As far as function definitions and class definitions are concerned, a definition is either a declaration or an expression.
12+
13+
Real (non-method) function:
14+
Arrow functions (only have an expression form)
15+
Traditional functions (created via function definitions)
16+
Generator functions (created via generator function definitions)
17+
Method:
18+
Methods (created by method definitions in object literals and class definitions)
19+
Generator methods (created by generator method definitions in object literals and class definitions)
20+
Constructor:
21+
Classes (created via class definitions)
22+
23+
*/
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 1. Prefer arrow functions as callbacks
2+
// 2. Prefer function declarations as stand-alone functions (versus callbacks)
3+
// 3. Prefer method definitions for methods
4+
5+
// ES5
6+
// MyClass.prototype.foo = function (arg1, arg2) {
7+
//
8+
// };
9+
10+
// ES6
11+
// Object.assign(MyClass.prototype, {
12+
// foo(arg1, arg2) {
13+
//
14+
// }
15+
// });
16+
17+
// Generator method definitions
18+
// const obj = {
19+
// *generatorMethod() {
20+
//
21+
// }
22+
// };
23+
//
24+
// class MyClass {
25+
// * generatorMethod() {
26+
//
27+
// }
28+
// }
29+
//
30+
// Calling a generator method returns a generator object.
31+
// You can use this and super as you would in normal method definitions;
32+
33+
// class Hello {
34+
// constructor() {
35+
// this.fn = () => {
36+
//
37+
// }
38+
// }
39+
//
40+
// showName = () => {
41+
//
42+
// }
43+
// }
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Three kinds of calls can be made anywhere in ES6:
3+
Function calls: func(3, 1)
4+
Method calls: obj.method('abc')
5+
Constructor calls: new Constr(8)
6+
*/
7+
8+
// Calls via super are restricted to specific locations
9+
10+
/*
11+
12+
Super-method calls: super.method('abc')
13+
Only available within method definitions inside either object literals or derived class definitions.
14+
Super-constructor calls: super(8)
15+
Only available inside the special method constructor() inside a derived class definition.
16+
17+
*/
18+
19+
// const obj = {
20+
// name: 'Vitalii',
21+
// showName: () => {
22+
// console.log(this);
23+
// }
24+
// };
25+
//
26+
// obj.showName();

ES6/Maps_and_Sets/maps.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Map is a data collection type (in a more fancy way — abstract data structure type), i
2+
// n which, data is stored in a form of pairs, which contains a unique key and value mapped to that key.
3+
// And because of the uniqueness of each stored key, there is no duplicate pair stored.
4+
// - key and value in Map can be in any data type, not limited to only string or integer.
5+
// - Map is mainly used for fast searching and looking up data.
6+
7+
// const map = new Map();
8+
// map.set('foo', 123);
9+
// map.get('foo'); // 123
10+
// map.has('foo'); // true
11+
// map.delete('foo'); // true
12+
// map.has('foo'); // false
13+
// map.size; // 1
14+
// map.clear(); // 0
15+
// map.size; // 0
16+
17+
// const map = new Map([
18+
// [ 1, 'one' ],
19+
// [ 2, 'two' ],
20+
// [ 3, 'three' ], // trailing comma is ignored
21+
// ]);
22+
//
23+
// console.log(map);
24+
25+
// Alternatively, the set() method is chainable:
26+
27+
// const map = new Map()
28+
// .set(1, 'one')
29+
// .set(4, 'one')
30+
// .set(2, 'two')
31+
// .set(3, 'three');
32+
//
33+
// console.log(map);
34+
35+
// 1. Any value can be a key, even an object
36+
// const map = new Map();
37+
//
38+
// const KEY1 = {};
39+
// map.set(KEY1, 'hello');
40+
// console.log(map.get(KEY1)); // hello
41+
42+
// const map = new Map();
43+
//
44+
// map.set(NaN, 123);
45+
// map.get(NaN);
46+
47+
// new Map().get('asfddfsasadf') // undefined

ES6/Maps_and_Sets/maps_iterating.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// const map = new Map([
2+
// [false, 'no'],
3+
// [true, 'yes'],
4+
// ]);
5+
6+
// for (const key of map.keys()) {
7+
// console.log(key);
8+
// }
9+
//
10+
// for (const key of map.values()) {
11+
// console.log(key);
12+
// }
13+
14+
// for (const entry of map.entries()) {
15+
// console.log(entry[0], entry[1]);
16+
// }
17+
18+
// for (const [key, value] of map.entries()) {
19+
// console.log(key, value);
20+
// }
21+
22+
// for (const [key, value] of map) {
23+
// console.log(key, value);
24+
// }
25+
26+
// 1. Converting iterables (incl. Maps) to Arrays
27+
// const map = new Map().set(false, 'no').set(true, 'yes');
28+
// console.log([...map.keys()]);
29+
// console.log([...map.values()]);
30+
// console.log([...map]);
31+
32+
// 2. Looping over Map entries
33+
// const map = new Map([
34+
// [false, 'no'],
35+
// [true, 'yes'],
36+
// ]);
37+
//
38+
// map.forEach((value, key, array) => {
39+
// console.log(key, value, array);
40+
// });
41+
42+
// 3. Filtering (or map) Maps
43+
// const originalMap = new Map()
44+
// .set(1, 'a')
45+
// .set(2, 'b')
46+
// .set(3, 'c');
47+
//
48+
// const filteredMap = new Map( // Convert the result back to a Map.
49+
// [...originalMap] // Convert the Map into an Array of [key,value] pairs
50+
// .filter(([k, v]) => k < 3) // Map or filter the Array.
51+
// );
52+
//
53+
// console.log(filteredMap);
54+
55+
// 4. Combining Maps
56+
// const map1 = new Map()
57+
// .set(1, 'a1')
58+
// .set(2, 'b1')
59+
// .set(3, 'c1');
60+
//
61+
// const map2 = new Map()
62+
// .set(2, 'b2')
63+
// .set(3, 'c2')
64+
// .set(4, 'd2');
65+
//
66+
// const combineMaps = new Map([...map1, ...map2]);
67+
// console.log(combineMaps); // or [...combineMaps] or JSON.stringify([...combineMaps] or new Map(JSON.parse(jsonStr));
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Differences
2+
// 1. Key fields => in Object either string or integer or symbols, in Map - any
3+
// 2. Element order => in Map original order of elements (pairs) is preserved, in Object - no
4+
// 3. Inheritance => Map is an instance of Object, but Object is definitely not an instance of Map.
5+
6+
// var map = new Map([[1,2],[3,4]]);
7+
// console.log(map instanceof Object); //true
8+
// var obj = new Object();
9+
// console.log(obj instanceof Map); //false

0 commit comments

Comments
 (0)