-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
281 lines (210 loc) · 5.9 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//nested function scope
// let a = 10;
// function outer() {
// let b = 20;
// function inner() {
// let c = 30;
// console.log(a, b, c);
// }
// inner();
// }
// outer();
//closure
/*
a closure is the combination of a function bundled together with references to its surrounding state.
closures are created every time a function is created, at function creation time.
in javascript, when we return a function from another function, we are effectively returning a combination
of the function definition along with the functions's scope. this would let the function definition
have an associated persistent memory which could hold on to live data between executions. that combination
of the function and its scope chain is what is called a closure in javascript.
*/
function outer() {
let counter = 0;
function inner() {
counter++;
console.log(counter);
}
return inner;
}
// const fn = outer();
// fn();
// fn();
//function currying
/*
currying is a process in functional programming in which we transform a function with multiple
arguments into a sequence of nested functions that take one argument at a time.
*/
function sum(a, b, c) {
return a + b + c;
}
// console.log(sum(2, 3, 5));
function curry(fn) {
return function (a) {
return function (b) {
return function (c) {
return fn(a, b, c);
};
};
};
}
const curriedSum = curry(sum);
// console.log(curriedSum(2)(3)(5));
const add2 = curriedSum(2);
const add3 = add2(3);
const add5 = add3(5);
// console.log(add5);
/* this
the javascript this keyword which is used in a function, refer to the object it belongs to
it makes functions reusable by letting you decide the object value
this value is determined entirely by how a function is called
order of precedence
-new binding
-explicit binding
-implicit binding
-default binding
*/
//implicit binding
const person = {
name: "liono",
sayMyName: function () {
console.log(`My name is ${this.name}`);
},
};
// person.sayMyName();
//explicit binding
function sayMyName() {
console.log(`My name is ${this.name}`);
}
// sayMyName.call(person);
//new binding
function Person(name) {
this.name = name;
}
const p1 = new Person("liono");
const p2 = new Person("tygra");
// console.log(p1.name,p2.name);
//default binding
globalThis.name = "cheetara";
// sayMyName();
/*prototype
*/
function Person(fName, lName) {
this.firstName = fName;
this.lastName = lName;
}
const person1 = new Person("Bruce", "Wayne");
const person2 = new Person("Peter", "Parker");
// person1.getFullName = function(){
// return `${this.firstName} ${this.lastName}`
// }
// console.log(person1.getFullName());
// console.log(person2.getFullName());
Person.prototype.getFullName = function () {
return `${this.firstName} ${this.lastName}`;
};
// console.log(person1.getFullName());
// console.log(person2.getFullName());
/*
prototypal inheritance
*/
function SuperHero(fName, lName) {
Person.call(this, fName, lName);
this.isSuperHero = true;
}
SuperHero.prototype = Object.create(Person.prototype);
SuperHero.prototype.constructor = SuperHero;
SuperHero.prototype.fightCrime = function () {
console.log("fighting crime");
};
const batman = new SuperHero("Bruce", "Wayne");
// console.log(batman.getFullName());
// batman.fightCrime();
/*
class
*/
class ClassPerson {
constructor(fName, lName) {
this.firstName = fName;
this.lastName = lName;
}
sayMyName() {
return `${this.firstName} ${this.lastName}`;
}
}
// const classPerson1 = new ClassPerson('Bruce', 'Wayne');
// console.log(classPerson1.sayMyName());
class ClassSuperHero extends ClassPerson {
constructor(fName, lName) {
super(fName, lName);
this.isSuperHero = true;
}
fightCrime() {
console.log("fighting crime");
}
}
const classBatman = new ClassSuperHero("Bruce", "Wayne");
// console.log(classBatman.sayMyName());
// classBatman.fightCrime();
/*
iterables and iterators
why
1-difficulty in accessing the element
2-difficult to manage iteration on the data for various types of data structures
there was a need to iterate over various data structures in a new way that abstracts away the complexity of
accessing elements one by one and was at the same time uniform across the different data structures
*/
const str = "lionothedeveloper";
for (let i = 0; i < str.length; i++) {
// console.log(str.charAt(i));
}
const arr = ["l", "i", "o", "n", "o"];
for (let i = 0; i < arr.length; i++) {
// console.log(arr[i]);
}
//for..of loop
for (const char of str) {
// console.log(char);
}
for (const item of arr) {
// console.log(item);
}
/*
...iterables and iterators
an object which implements the iterable protocol is called iterable
for an object to be an iterable it must implement a method at the key [Symbol.iterator]
that method should not accept any argument and should return an object which conforms to the iterator protocol
the iterator protocol decides whether an object is an iterator
the object must have a next() method that returns an objects with two properties
value:which gives the current element
done:which is a boolean value indicating whether or not there are more elements that could be iterated upon
*/
const obj = {
[Symbol.iterator]: function () {
let step = 0;
const iterator = {
next: function () {
step++;
if (step === 1) {
return { value: "hello", done: false };
} else if (step === 2) {
return { value: "friend", done: false };
}
return { value: undefined, done: true };
},
};
return iterator;
},
};
// for(step of obj){
// console.log(step);
// }
// Generators another and easier way to implement iterator.
function* generatorFunction(){
yield "Hello";
yield "World";
yield "Whats up!";
}
const generatorObj = generatorFunction();
for(const item of generatorObj){
console.log(item);
}