-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
315 lines (256 loc) · 7.39 KB
/
index.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// This is a long JavaScript file that adheres to common ESLint and Prettier configurations.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
static compareAges(person1, person2) {
if (person1.age > person2.age) {
return `${person1.name} is older than ${person2.name}.`;
} else if (person1.age < person2.age) {
return `${person2.name} is older than ${person1.name}.`;
} else {
return `${person1.name} and ${person2.name} are the same age.`;
}
}
}
const john = new Person("John", 30);
const jane = new Person("Jane", 25);
console.log(john.greet());
console.log(jane.greet());
console.log(Person.compareAges(john, jane));
// Async/Await example
async function fetchData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
return data;
} catch (error) {
console.error("There was a problem with the fetch operation:", error);
}
}
const apiUrl = "https://jsonplaceholder.typicode.com/posts";
fetchData(apiUrl).then(data => {
console.log("Fetched data:", data);
});
// Higher-order functions example
function processArray(arr, callback) {
return arr.map(callback);
}
const numbers = [1, 2, 3, 4, 5];
const doubled = processArray(numbers, number => number * 2);
console.log("Original numbers:", numbers);
console.log("Doubled numbers:", doubled);
// Example of closures
function makeCounter() {
let count = 0;
return function () {
count += 1;
return count;
};
}
const counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2
// Example of a Promise
function asyncOperation() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Operation completed");
}, 2000);
});
}
asyncOperation()
.then(result => {
console.log(result);
})
.catch(error => {
console.error("Error:", error);
});
// Example of using template literals
const name = "Alice";
const age = 28;
const introduction = `My name is ${name} and I am ${age} years old.`;
console.log(introduction);
// Functional programming example
const numbersArray = [1, 2, 3, 4, 5];
const sum = numbersArray.reduce(
(accumulator, current) => accumulator + current,
0
);
console.log("Sum of numbers:", sum);
// Example of a simple class with private fields (using the # syntax)
class BankAccount {
#balance;
constructor(initialBalance) {
this.#balance = initialBalance;
}
deposit(amount) {
if (amount > 0) {
this.#balance += amount;
console.log(`Deposited ${amount}. New balance: ${this.#balance}`);
} else {
console.log("Deposit amount must be positive.");
}
}
withdraw(amount) {
if (amount > 0 && amount <= this.#balance) {
this.#balance -= amount;
console.log(`Withdrew ${amount}. New balance: ${this.#balance}`);
} else {
console.log("Insufficient balance or invalid amount.");
}
}
getBalance() {
return this.#balance;
}
}
const myAccount = new BankAccount(100);
myAccount.deposit(50);
myAccount.withdraw(30);
console.log("Final balance:", myAccount.getBalance());
// Array destructuring example
const [first, second, ...rest] = [10, 20, 30, 40, 50];
console.log("First:", first);
console.log("Second:", second);
console.log("Rest:", rest);
// Object destructuring example
const user = {
id: 1,
name: "Bob",
email: "[email protected]"
};
const { name: userName, email: userEmail } = user;
console.log("User Name:", userName);
console.log("User Email:", userEmail);
// Example of a Map
const map = new Map();
map.set("key1", "value1");
map.set("key2", "value2");
map.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
// Example of a Set
const set = new Set([1, 2, 2, 3, 4, 4]);
console.log("Set values:", [...set]);
// Example of a RegExp
const regex = /hello/i;
const str = "Hello World!";
console.log("Does the string match the regex?", regex.test(str));
// Example of an immediately-invoked function expression (IIFE)
(function () {
const message = "I am an IIFE!";
console.log(message);
})();
// Example of a generator function
function* idGenerator() {
let id = 1;
while (true) {
yield id++;
}
}
const generator = idGenerator();
console.log(generator.next().value); // 1
console.log(generator.next().value); // 2
// Example of an object with shorthand properties and methods
const a = 10;
const b = 20;
const obj = {
a,
b,
sum() {
return this.a + this.b;
}
};
console.log("Object sum:", obj.sum());
// Example of a Symbol
const uniqueId = Symbol("id");
const objWithSymbol = {
[uniqueId]: 123,
name: "Symbol Example"
};
console.log("Object with Symbol property:", objWithSymbol);
// Example of a Proxy
const handler = {
get(target, prop, receiver) {
return prop in target ? target[prop] : "Property does not exist";
}
};
const target = { a: 1, b: 2 };
const proxy = new Proxy(target, handler);
console.log(proxy.a); // 1
console.log(proxy.c); // Property does not exist
// Example of a class extending another class
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a noise.`;
}
}
class Dog extends Animal {
speak() {
return `${this.name} barks.`;
}
}
const dog = new Dog("Rex");
console.log(dog.speak());
// Example of a class with static methods and properties
class MathUtil {
static PI = 3.14159;
static circleArea(radius) {
return MathUtil.PI * radius * radius;
}
}
console.log("Circle area:", MathUtil.circleArea(5));
// Example of spread operator
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2];
console.log("Combined array:", combined);
// Example of rest parameters
function sumAll(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log("Sum of all numbers:", sumAll(1, 2, 3, 4, 5));
// Example of an arrow function
const add = (a, b) => a + b;
console.log("Addition result:", add(2, 3));
// Example of an async function with try/catch
async function fetchAndProcessData(url) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error("Failed to fetch");
const data = await response.json();
console.log("Data processed:", data);
} catch (error) {
console.error("Error:", error);
}
}
fetchAndProcessData("https://jsonplaceholder.typicode.com/users");
// Example of using the Date object
const now = new Date();
console.log("Current Date and Time:", now.toString());
// Example of using the `typeof` operator
console.log("Type of 42:", typeof 42);
console.log("Type of {}:", typeof {});
// Example of a basic setInterval and setTimeout usage
setTimeout(() => {
console.log("This will run after 2 seconds");
}, 2000);
let counterInterval = 0;
const intervalId = setInterval(() => {
console.log("Interval count:", counterInterval);
counterInterval += 1;
if (counterInterval === 5) {
clearInterval(intervalId);
}
}, 1000);
console.log("End of the script");