-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
467 lines (379 loc) · 11.5 KB
/
index.html
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<html>
<head>
<title>JavaScript Tricks</title>
</head>
<body>
<h1>Execute in Console</h1>
<script>
const { log } = console;
// 1. No Concatenation use Template string
{
let name = 'Charlse';
let place = 'India';
let isPrime = bit => {
return (bit === 'P' ? 'Prime' : 'Nom-Prime');
}
let messageConcat = 'Mr. ' + name + ' is from ' + place + '. He is a' + ' ' + isPrime('P') + ' member.'
log(messageConcat);
let messageTemplateStr = `Mr. ${name} is from ${place}. He is a ${isPrime('P')} member.`
log(messageTemplateStr);
}
// 2. isNumber
{
let mynum = 123;
let mynumStr = "123";
let myNumFloat = 123.45;
log(`${mynum} is a number?`, Number.isInteger(mynum));
log(`${mynumStr} is a number?`, Number.isInteger(mynumStr));
log(`${myNumFloat} is a number?`, Number.isInteger(myNumFloat));
}
// 3. Value as Number
function trackChange(event) {
let value = event.target.value;
log(`is ${value} a number?`, Number.isInteger(value));
let valueAsNumber = event.target.valueAsNumber;
log(`is ${value} a number?`, Number.isInteger(valueAsNumber));
}
// 4. Short hand with AND
{
let isPrime = true;
const startWatching = () => {
log('Started Watching!');
}
if (isPrime) {
startWatching();
}
isPrime && startWatching();
}
// 5. Default values with OR
{
let person = {name: 'Jack'};
let age = person.age || 35;
log(`Age of ${person.name} is ${age}`);
}
// 6. Randoms
{
let planets = ['Mercury ', 'Mars', 'Venus', 'Earth', 'Neptune', 'Uranus', 'Saturn', 'Jupiter'];
let randomPlanet = planets[Math.floor(Math.random() * planets.length)];
log('Random Planet', randomPlanet);
let getRandom = (min, max) => {
return Math.round(Math.random() * (max - min) + min);
}
log('Get random', getRandom(0, 10));
}
// 7. Function default params
{
let greetings = (name, message='Hello,') => {
return `${message} ${name}`;
}
log(greetings('Jack'));
log(greetings('Jack', 'Hola!'));
}
// 8. Required Function Params
{
let isRequired = () => {
throw new Error('This is a mandatory parameter.');
}
let greetings = (name=isRequired(), message='Hello,') => {
return `${message} ${name}`;
}
log(greetings('Jack'));
}
// 9. Comma Operator
{
let count = 1;
let ret = (count++, count);
log(count);
}
// 10. Merging multiple objects
{
let emp = {
'id': 'E_01',
'name': 'Jack',
'age': 32,
'addr': 'India'
};
let job = {
'title': 'Software Dev',
'location': 'Paris'
};
// spread operator
let merged = {...emp, ...job};
log('Spread merged', merged);
log('Object assign', Object.assign({}, emp, job));
// for deep merge use _merge of lodash
}
// 11. Destructuring
{
// array
let emojis = ['🔥', '⏲️', '🏆', '🍉'];
let [fire, clock, , watermelon] = emojis;
log(fire, clock, watermelon);
let [f, ...rest] = emojis;
log(f);
log(rest);
// object
let shape = {
name: 'rect',
sides: 4,
height: 300,
width: 500
};
let {name, sides, ...restObj} = shape;
log(name, sides);
log(restObj);
}
// 12. Swap variables
{
let fire = '🔥';
let fruit = '🍉';
[fruit, fire] = [fire, fruit];
log(fire, fruit);
}
// 13. isArray
{
let emojis = ['🔥', '⏲️', '🏆', '🍉'];
log(Array.isArray(emojis));
let obj = {};
log(Array.isArray(obj));
}
// 14. undefined vs null
{
undefined == null // true
undefined === null // false
}
// 15. Get Query Params
{
let project = new URLSearchParams(location.search).get('project');
}
// 16. Tagged Template Literals
{
function introduce(strings, ...values) {
let msg =
`<span style="color:${values[1]}">
Hello ${values[0]}, Have a Nice Day! We know your favorite color is <u>${values[1]}</u>
</span>`;
return msg;
}
const name = 'Joe';
const color = 'green';
const message = introduce`Hello, I'm ${name} and ${color} is my favorite color!`;
log(message);
document.body.innerHTML = message;
}
// 17. Destructure and pick a property
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const { name } = user;
console.log(name);
}
// 18. Declaration Gotcha
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
{ name } = user // Uncaught SyntaxError: Unexpected token '='
let name;
{ name } = user; // user // Uncaught SyntaxError: Unexpected token '='
({ name } = user)
console.log(name); // Alex
}
// 19. Desctructure and pick multiple properties
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const { name, age } = user;
console.log(name, age);
}
// 20. Assign new variable and default value
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const {name, age, salary=123455} = user;
console.log(name, age, salary)
}
// 21. Destructure and add aliases
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const { address: permanentAddress } = user;
console.log(permanentAddress);
}
// 22. Destructuring nested objects
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43,
'department':{
'name': 'Sales',
'Shift': 'Morning',
'address': {
'city': 'Bangalore',
'street': '7th Residency Rd',
'zip': 560001
}
}
}
const { department } = user; // {name: "Sales", Shift: "Morning", address: {…}}
const { department: { address } } = user; // {city: "Bangalore", street: "7th Residency Rd", zip: 560001}
const { department: { address: { city } } } = user; // Bangalore
}
// 23. Dynamic name property
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const getValue = key => {
const { [key]: returnValue } = user;
return returnValue;
}
getValue('name') // Alex's
getValue('age') // 43
}
// 24. Destructuring into Function Parameter
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
logDetails(user); // Alex is 43 year(s) old!
function logDetails({name, age}) {
console.log(`${name} is ${age} year(s) old!`)
}
}
// 25. Destructure function return
{
const getUser = () => {
return{
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
}
const { name, address, age } = getUser();
console.log(name, age, address);
}
// 26. Destructuring in loops
{
const users = [{
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
},
{
'name': 'Bob',
'address': 'Canada',
'age': 53
},
{
'name': 'Carl',
'address': 'Bangalore',
'age': 26
}];
for(let { name, age } of users) {
console.log(name)
}
}
// 27. Destructure console.log
{
const { log, warn, error } = console;
}
// 28. Create a clone of an object
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const clone = {...user} // {name: "Alex", address: "15th Park Avenue", age: 43}
clone === user; // false
}
// 29. Add properties to an object in Immutable way
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const updatedUser = {...user, salary:12345}; // {name: "Alex", address: "15th Park Avenue", age: 43, salary: 12345}
console.log(user); // {name: "Alex", address: "15th Park Avenue", age: 43}
}
// 30. Update an object in Immutable way
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const updatedUser = {...user, age:56}; // {name: "Alex", address: "15th Park Avenue", age: 56}
console.log(user); // {name: "Alex", address: "15th Park Avenue", age: 43}
}
// 31. Update Nested objects
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43,
'department':{
'name': 'Sales',
'Shift': 'Morning',
'address': {
'city': 'Bangalore',
'street': '7th Residency Rd',
'zip': 560001
}
}
}
{...user, department: {'number': 006}} // department replaced
{...user, department: {...user.department, 'number': 006}} // Right way
}
// 32. Combine two objects - Shallow Merge
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const department = {
'name': 'Sales',
'Shift': 'Morning'
}
const completeDetails = {...user, ...department};
console.log({completeDetails});
}
// 33. Rest operator in Object Destructuring
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const {name, ...rest} = user;
console.log(name, rest);
const {age, ...rest} = user;
console.log(age, rest);
}
</script>
<br />
<input type='number' onkeyup="trackChange(event)" />
</body>
</html>