-
Notifications
You must be signed in to change notification settings - Fork 0
/
essentials.js
executable file
·1409 lines (1087 loc) · 43.6 KB
/
essentials.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* JS ESSENTIALS */
//in-line comment
/* this is a
multi-line
comment */
/* VARIABLES ------------------------------------------------------------------------------------------------------------------- */
var myName = "Lucas"; //functional or globed scoped
let myAge = 31; //block scoped
const siete = 7; //block scoped + cannot get new value
/* DATA TYPES ------------------------------------------------------------------------------------------------------------------- */
// Primitives (immutable data types)
let dataString = "This is a string";
let dataNumber = 20;
let dataBigInt = 9007199254740991n || BigInt(9007199254740991);
let dataBoolean = true || false;
let dataUndefined = undefined;
let dataSymbol = Symbol('foo');
let dataNull = null;
// Non-primitives (mutable data types)
let dataObject = {key: 'value', method(){"Hello"}};
let dataArray = [1, 2, 3];
// Typeof operator
typeof dataString; //string
typeof dataNumber; //number
typeof dataBigInt; //bigint
typeof dataBoolean; //boolean
typeof dataUndefined; //undefined
typeof dataSymbol; //symbol
typeof dataNull; //object
typeof dataObject; //object
typeof dataArray; //object
// Type coercion
const numero1 = 20; //type number
const numero2 = '40'; //type string
console.log(numero1 + numero2); //60
/* OPERATORS ------------------------------------------------------------------------------------------------------------------- */
/*
// Basic Operators
+ — Addition
- — Subtraction
* — Multiplication
/ — Division
% — Remainder
++ — Increment numbers
-- — Decrement numbers
= — Asignment operator
(...) — Grouping operator: operations within brackets are execute earlier than those outside
// Comparison Operators
== — Equal to
=== — Equal value and equal type
!= — Not equal
!== — Not equal value or not equal type
> — Greater than
< — Less than
>= — Greater than or equal to
<= — Less than or equal to
? — Ternary operator
// Logical Operators
&& — Logical and
|| — Logical or
! — Logical not
// Bitwise Operators
& — AND // 5 & 1 (0101 & 0001) 1 (1)
| — OR // 5 | 1 (0101 | 0001) 5 (101)
~ — NOT // ~ 5 (~0101) 10 (1010)
^ — XOR // 5 ^ 1 (0101 ^ 0001) 4 (100)
<< — Left shift // 5 << 1 (0101 << 1) 10 (1010)
>> — Right shift // 5 >> 1 (0101 >> 1) 2 (10)
>>> — Zero fill right shift // 5 >>> 1 (0101 >>> 1) 2 (10)
*/
// Incrementing and Decrementing numbers
let myVar = 1;
myVar++;
myVar--;
// Compound Assignment with Augmented
var a = 3;
var b = 17;
var c = 12;
var d = 25;
a += 12; // Output: 15
b -= 5; // Output: 12
c *= 2; // Output: 24
d /= 5; // Output: 5
// Strict Equality Comparison
console.log(3 == '3'); //true
console.log(3 === '3'); //false (does not convert types of data)
console.log(2 != '2'); //false
console.log(2 !== '2'); //true (does not convert types of data)
// Nullish coalescing operator
const valor1 = 0 || 'default'; // output: 'default'
const valor2 = '' || 1000; // output: '1000'
const valor3 = undefined || 'default'; // output: 'default'
const valor4 = null || 'default'; // output: 'default'
const valor5 = 0 ?? 'default'; // output: 0
const valor6 = '' ?? 1000; // output: ''
const valor7 = undefined ?? 'default'; // output: default
const valor8 = null ?? 'default'; // output: null
/* CONDITIONALS ------------------------------------------------------------------------------------------------------------------- */
// if else statements
let a = 1;
let b = 2;
if (a < b) {
console.log('is true!');
} else {
console.log('is false!');
}
// Multi if else statements
let val = 8;
if (val > 10) {
console.log("Greater than 10");
} else if (val < 5) {
console.log("Smaller than 5");
} else {
console.log("Between 5 and 10");
}
// Ternary operators (same as if else)
let uno = 1;
let dos = 2;
const isEqual = uno === dos ? true : false;
console.log(isEqual); //false
// Switch statements
function switchCase(n) {
switch(n) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
case 5:
answer = "delta";
break;
default:
answer = "default";
break;
}
return answer;
}
console.log(switchCase(4)); //Outputs 'delta'
/* LOOPS ------------------------------------------------------------------------------------------------------------------- */
// While loop
let i = 0;
while (i < 10) {
console.log(i); // Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
i++;
}
// Do while loop
let j = 0;
do {
console.log(j); // Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
j++;
} while (j < 10)
// For loop
for (let i = 0; i < 10; i++) {
console.log(i); // Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
// For in / for of
const arr = [3, 5, 7, 'foo'];
for (let item of arr) {
console.log(item); // Output: 3, 5, 7, 'foo'
}
for (let index in arr) {
console.log(index); // Output: '0', '1', '2', '3'
}
let object = {
age: 20,
city: 'Buenos Aires'
};
for(let key in object){
console.log(key); // Output: 'age', 'city'
}
// Break / continue statements
for(i = 0; i < 10; i++){
if(i == 3){
continue;
}
if(i == 7){
break;
}
console.log(i); // Output: 1, 2, 4, 5, 6
}
/* FUNCTIONS ------------------------------------------------------------------------------------------------------------------- */
// Function declaration (call the function before declaration is possible (hoisting))
sum(2, 3); // Outputs 5
function sum(a, b) {
return a + b;
}
// Function expression (call the function before declaration is not possible)
f('word'); // Error
const f = function(w){
return w
}
// Arrow functions
const f1 = () => {}; //Zero parameters
const f2 = paramOne => {}; //One parameter
const f3 = (paramOne, paramTwo) => {}; //Two or more parameters
const double = number => number * 2; //Single-line block
// Arrow functions and 'this'
const myObject2 = {
myMethod2: function(){
console.log(this); // logs myObject2
}
}
myObject2.myMethod2();
const myObject = {
myMethod: () => {
console.log(this); // logs Window object
}
};
myObject.myMethod(); //Arrow functions do not bind their own 'this', instead, they inherit the one from the parent scope (lexical scoping).
// Scope
let outerWear = "T-Shirt"; //Global scope
function myOutfit() {
let outerWear = "sweater"; //Local scope
return outerWear;
}
console.log(myOutfit()); // Outputs "sweater"
console.log(outerWear); // Outputs "T-Shirt"
// Default parameters in functions
function increment(arg1, arg2 = 1) {
return arg1 + arg2;
};
console.log(increment(5)); // Outputs 6
console.log(increment(5, 2)); // Outputs 7
// Spread Operator and Rest Parameters
function sum(...args){
console.log('You have passed ' + args.length + ' arguments'); // You have passed 6 arguments
return args.reduce((a, b) => a + b);
}
var numbers = [1, 2, 3];
console.log(sum(...numbers, 4, 5, 6)); // Output: 21
// Closures
function getCounter(){
let counter = 0;
return function(){
return counter++;
}
}
let contador = getCounter();
console.log(contador()); // 0
console.log(contador()); // 1
console.log(contador()); // 2
console.log(contador()); // 3
// Callbacks
function callMe() { // callback function
console.log('I am callback function');
}
function greet(name, callback) {
console.log(`Hi ${name}`);
callback();
}
greet('Peter', callMe); // passing function as an argument
// Generator
function* generator(i) {
yield i;
yield i + 10;
}
const gen = generator(10);
console.log(gen.next().value); // expected output: 10
console.log(gen.next().value); // expected output: 20
// Currying
const suma = (a, b, c) => a + b + c;
const parcial = a => b => c => suma(a, b, c);
console.log(parcial(1)(2)(3)); // 6
// Composition
const getName = info => ({
showName() {
console.log(`Nombre: ${info.name}`)
}
})
function User(name, age){
let info = {
name,
age
}
return Object.assign(info, getName(info))
}
let user = User('Lucas', 30);
user.showName();
// Explicit Binding
function persona(item1, item2){
console.log(`Mi nombre es ${this.nombre} y escucho ${item1} y ${item2}`);
}
const info = {
nombre: 'Luna'
}
const musica = ['Reggae', 'Rock'];
persona.call(info, musica[0], musica[1]);
persona.apply(info, musica);
const nuevaFn = persona.bind(info, musica[0], musica[1]);
nuevaFn();
/* ARRAYS ------------------------------------------------------------------------------------------------------------------- */
let dogs = ['Bulldog', 'Beagle', 'Labrador'];
const numbers = [2, 5, 8, 7, 1, 6, 10, 3, 4, 9];
// Nested Arrays
let nestedArray = [[10, 20], [30, 40], ['strings', 60]];
// Length property
dogs.length; // Output: 3
// Accessing Array elements
console.log(numbers[3]); // Output: 7
console.log(nestedArray[1][0]); // Output: 30
// Modify Array data with indexes
nestedArray[2][0] = 45; // nestedArray now equals [[10, 20], [30, 40], [45, 60]]
// Spread operator
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
console.log(arr2); // Output: [1, 2, 3, 4, 5, 6]
// Destructuring assignment
const array = [1, 2, 3, 4, 5, 6];
const [w, x, , ...z] = array; // ignore index 2
console.log(w, x, z); // Outputs 1, 2, [4, 5, 6]
// Destructuring assignment to switching places in arrays
let a = 8, b = 6;
[a, b] = [b, a]; // a = 6, b = 8
// Destructuring assignment with default values
let [named = "Guest", surname = "Anonymous"] = ["Julius"];
console.log(named); // Julius (from array)
console.log(surname); // Anonymous (default used)
// Deep clone arrays
let array1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let array2 = [...array1]; //Using Spread Operator
let array3 = array1.slice(); //Using slice() method
let array4 = JSON.parse(JSON.stringify(array1)); //Using JSON.parse and JSON.stringify
// Array Methods
let myArray = Array(7); // Output: [empty x 7]. Creates an array with 7 empty slots.
let arrayOf = Array.of(7, 8, 9); // Output: [7, 8, 9]. Creates an array.
let arrayFrom = Array.from('foo'); // Output: ["f", "o", "o"].
let penultimateElement = dogs.at(-2); // Output: 'Beagle'.
let animals = dogs.concat('cats', 'birds'); // Output: ["Bulldog", "Beagle", "Pug", "Boxer", "Labrador", "cats", "birds"]
let every = numbers.every(n => n < 11); // Outputs: true. All elements in the array pass the test. Returns a boolean value.
let filtered = numbers.filter(x => x > 3); // Output: [5, 8, 7, 6, 10, 4, 9]
let find = numbers.find(n => n > 5); // Output: 8. Returns the value of the first element in the array that passes the test.
let findIndex = numbers.findIndex(n => n > 5); // Output: 2. Returns the index of the first element in the array that passes the test.
dogs.indexOf('Beagle'); // Output: 1
dogs.includes('Labrador'); // Output: true. Returns a boolean value.
let joined = dogs.join(' * '); // Output: "Bulldog * Beagle * Labrador"
let maped = numbers.map(x => x * 2); // Output: [4, 10, 16, 14, 2, 12, 20, 6, 8, 18]
let popped = dogs.pop(); // Remove last element. dogs = ['Bulldog', 'Beagle']. popped = 'Labrador'
dogs.push('Chihuahua'); // Add new element to the end
let reduced = numbers.reduce((x, y) => x + y); // Output: 55
dogs.reverse(); // Reverse unicode sorting
let shifted = dogs.shift(); // Remove first element. dogs = ['Beagle', 'Labrador']. shifted = 'Bulldog'
let sliced = dogs.slice(1, 3); // Pulls a copy of a portion of an array into a new array
let sameDogs = dogs.slice(); // Deep copy
let some = numbers.some(n => n > 9); // Output: true. At least one element in the array passes the test. Returns a boolean value.
dogs.sort(); // Output: ["Beagle", "Bulldog", "Labrador"]. Unicode sorting.
numbers.sort((a, b) => a - b); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. Numeric sort.
numbers.sort((a, b) => b - a); // Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]. Numeric descending sort.
numbers.sort((a, b) => 0.5 - Math.random()); // Random order sort.
dogs.splice(2, 0, 'Pug', 'Boxer'); // Modify Array (where, how many to remove, add elements).
dogs.toString(); // Convert to string, Output: "Bulldog,Beagle,Labrador".
dogs.unshift('Dogo'); // Add new element to the beginning.
/* STRINGS ------------------------------------------------------------------------------------------------------------------- */
let str = "Hello";
// Concatenation
let str2 = 'World!';
console.log(str + ' ' + str2); // Output: Hello World!
// Concatenating Strings with plus equals operator
let ourStr = "I come first. ";
console.log(ourStr += "I come second. "); // Output: "I come first. I come second. "
// String Interpolation
const myPet = 'cat';
console.log(`I own a pet ${myPet}.`); // Output: I own a pet cat.
// Length property
str.length; // Output: 5
// Access character in string
let lastName = "Lovelace";
let firstLetterOfLastName = lastName[0]; //Output: "L"
let lastLetterOfLastName = lastName[lastName.length - 1]; //Output: "e"
/* Escape Sequences in Strings
\' single quote
\" double quote
\\ backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed
*/
// Strings Methods
str.charAt(1); // Output: e. Character at index 2.
str.charCodeAt(2); // Output: 108. Unicode number.
str.concat(" Wor", "ld"); // Output: "Hello World".
str.indexOf("e"); // Output: 1. Find substring, -1 if doesn't contain.
str.includes("llo"); // Output: true. Return true if the search string is found within the given string, false if not.
str.endsWith("lo"); // Output: true. True if the given characters are found at the end of the string; otherwise, false.
str.lastIndexOf("l"); // Output: 3. Last occurance.
str.match(/[A-H]/gi); // Output: ["H", "e"]. (Support RegExp).
str.repeat(3); // Output: "HelloHelloHello".
str.replace("H", "123"); // Output: "123ello". Find and replace. (Support RegExp).
str.search("l"); // Output: 2. The index of the first match, or -1 if no match was found. (Support RegExp).
str.slice(1, 4); // Output: "ell". Cuts a portion of string. Negative values count from behind.
str.split(""); // Output: ["H", "e", "l", "l", "o"]. (Support RegExp).
str.split(" "); // Output: ["Hello"].
str.startsWith("H"); // Output: true. True if the given characters are found at the beginning of the string; otherwise, false.
str.toLowerCase(); // Convert to lower case.
str.toUpperCase(); // Output: "HELLO". Convert to upper case.
str.trim(); // Trim whitespaces from left and right side.
str.trimStart(); // Trim whitespace from left side.
str.trimEnd(); // Trim whitespace form right side.
/*REGULAR EXPRESSION SYNTAX ------------------------------------------------------------------------------------------------------------------- *//*
// Pattern Modifiers (flags)
e //Evaluate replacement
i //Case-insensitive
g //Global match
m //Multiple lines
s //Treat strings as single line
x //Allow comments and whitespace in pattern
U //Ungreedy pattern
// Groups and ranges
[abc] //Any of the characters between the brackets
[^abc] //Any character not in the brackets
[0-9] //Any digit from 0 to 9
[A-z] //Any character from uppercase A to lowercase z
(a|b|c) //Find any of the alternatives separated with |
[:punct:] //Any punctuation symbol
[:space:] //Any space character
[:blank:] //Space or tab
// Character classes
. //Any single character, except newline or line terminator
\d //A digit
\D //A non-digit character
\w //Word character
\W //Non-word character
\s //Whitespace character
\S //Non-whitespace character
\t //Tab character
\r //Carriage return character
\n //A new line character
\v //Vertical tab character
\f //Form feed character
[\b] //A backspace
\0 //NUL character
\xxx //The character specified by an octal number xxx
\xdd //Character specified by a hexadecimal number dd
\uxxxx //The Unicode character specified by a hexadecimal number xxxx
\ //Escape character
// Quantifiers
a+ //One or more of a
a* //Zero or more occurrences of a
a? //Zero or one occurrences of a
a+? //One or more, ungreedy
a*? //Zero or more, ungreedy
?=a //Any string that is followed by a specific string a
?!a //String that is not followed by a specific string a
a{2} //Exactly 2 of a
a{2,} //2 or more of a
a{,5} //Up to 5 of a
a{2,5} //2 to 5 of a
// Assertions
\b //Find a match at the beginning / end of a word
\B //Find a match not at the beginning / end of a word
^a //Any string with "a" at the beginning of it
a$ //Any string with "a" at the end of it
x(?=y) //Matches "x" only if "x" is followed by "y".
x(?!y) //Matches "x" only if "x" is not followed by "y".
(?<=y)x //Matches "x" only if "x" is preceded by "y".
(?<!y)x //Matches "x" only if "x" is not preceded by "y".
*/
/* BOOLEANS ------------------------------------------------------------------------------------------------------------------- */
// truthy and falsy values
//falsy
let falsy1 = false;
let falsy2 = 0; //zero
let falsy3 = -0; //minus zero
let falsy4 = 0n; //BigInt zero
let falsy5 = '' || "" || ``; //empty string
let falsy6 = null;
let falsy7 = undefined;
let falsy8 = NaN;
//truthy
let truthy1 = '0'; //a string containing a single zero
let truthy2 = 'false'; //a string containing the text 'false'
let truthy3 = []; //an empty array
let truthy4 = {}; //an empty object
let truthy5 = function(){}; //an 'empty' function
/* OBJECTS ------------------------------------------------------------------------------------------------------------------- */
// Object literal
let student = {
firstName:"Jane", // properties (key: value)
lastName:"Doe",
age:18,
height:170,
fullName() { // method (function)
return this.firstName + " " + this.lastName; //the value of 'this' keyword is the calling object.
}
};
// Accesing objects
student.firstName; // Dot notation. Output: Jane
student['lastName']; // Bracket notation. Output: Doe
student.fullName(); // Output: Jane Doe.
// Updating Object Properties
student.age = 19; // Dot notation.
student['height'] = 180; // Bracket notation.
// Add new properties to an Object
student.hobbie = 'guitar'; // Dot notation.
student['favourite food'] = 'pizza'; // Bracket notation.
// Delete operator
delete student.age;
// Deep clone
let obj = { name: 'luna', age: 5 };
let clon2 = { ...obj }; // Using spread operator
let clon3 = JSON.parse(JSON.stringify(obj)); // Using JSON.parse and JSON.stringify
// Accessing Nested Objects
var myStorage = {
car: {
inside: {
'glove box': 'maps',
seats: 'crumbs'
}
}
};
var gloveBoxContents = myStorage.car.inside['glove box']; //Outputs 'maps'
// Optional chaining
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName); // output: undefined
console.log(adventurer.someNonExistentMethod?.()); // expected output: undefined
// Privacy
// Getters
const robot = {
_energyLevel: 100,
get energyLevel(){ //Getters can perform an action on the data when getting a property.
if(typeof this._energyLevel === 'number') { //Getters can return different values using conditionals.
return 'My current energy level is ' + this._energyLevel
} else {
return "System malfunction: cannot retrieve energy level"
}
}
};
console.log(robot.energyLevel); //Output: My current energy level is 100
// Setters
const person = {
_age: 37,
set age(newAge){ //Setters can check input and perform actions on properties.
if (typeof newAge === 'number'){
this._age = newAge;
} else {
console.log('You must assign a number to age');
}
}
};
person.age = 40;
console.log(person._age); // Output: 40
person.age = '40'; // Output: You must assign a number to age
// Create Objects with functions with property value shorthand
const monsterFactory = (name, age, catchPhrase) => {
return {
name,
age,
scare() {
console.log(catchPhrase);
}
}
};
const ghost = monsterFactory('Casper', 251, 'BOO!'); //Output: {name: "Casper", age: 251, scare: ƒ}
// Destructured assignment
const vampire = {
name: 'Dracula',
residence: 'Transylvania',
tastes: {
drink: 'Blood',
food: 'Burger'
}
};
const { residence } = vampire; // residence outputs 'Transylvania'
const { name: newName } = vampire; // newName outputs 'Dracula'
const { tastes: {drink, food} } = vampire; // drink outputs 'Blood', food outputs 'Burger'
// Destructuring Assignment to pass an Object as a Function's Parameters
const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};
function half({max, min}) {
return (max + min) / 2.0;
}
console.log(half(stats));
// Static methods (built-in)
Object.keys() //Returns an array containing the names of all of the given object's own enumerable string properties.
Object.values() //Returns an array containing the values that correspond to all of a given object's own enumerable string properties.
Object.entries() //Returns an array containing all of the [key, value] pairs of a given object's own enumerable string properties.
Object.assign() //Copies the values of all enumerable own properties from one or more source objects to a target object.
Object.create() //Creates a new object with the specified prototype object and properties.
Object.defineProperty() //Adds the named property described by a given descriptor to an object.
Object.defineProperties() //Adds the named properties described by the given descriptors to an object.
Object.freeze() //Freezes an object. Other code cannot delete or change its properties.
Object.seal() //Prevents other code from deleting properties of an object.
Object.fromEntries() //Returns a new object from an iterable of [key, value] pairs. (This is the reverse of Object.entries).
Object.getOwnPropertyDescriptor() //Returns a property descriptor for a named property on an object.
Object.getOwnPropertyDescriptors() //Returns an object containing all own property descriptors for an object.
Object.getOwnPropertyNames() //Returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.
Object.getOwnPropertySymbols() //Returns an array of all symbol properties found directly upon a given object.
Object.getPrototypeOf() //Returns the prototype (internal [[Prototype]] property) of the specified object.
Object.is() //Compares if two values are the same value. Equates all NaN values (which differs from both Abstract Equality Comparison and Strict Equality Comparison).
Object.isExtensible() //Determines if extending of an object is allowed.
Object.isFrozen() //Determines if an object was frozen.
Object.isSealed() //Determines if an object is sealed.
Object.preventExtensions() //Prevents any extensions of an object.
Object.setPrototypeOf() //Sets the object's prototype (its internal [[Prototype]] property).
// Instance methods (built-in)
Object.hasOwnProperty() //Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
Object.isPrototypeOf() //Returns a boolean indicating whether the object this method is called upon is in the prototype chain of the specified object.
Object.propertyIsEnumerable() //Returns a boolean indicating if the internal ECMAScript [[Enumerable]] attribute is set.
Object.toLocaleString() //Calls toString().
Object.toString() //Returns a string representation of the object.
Object.unwatch() //Removes a watchpoint from a property of the object.
Object.valueOf() //Returns the primitive value of the specified object.
Object.watch() //Adds a watchpoint to a property of the object.
/* CLASSES ------------------------------------------------------------------------------------------------------------------- */
class Dog { //By convention, we capitalize and camelCase class names.
constructor(name) { //constructor method
this._name = name;
}
get name() {
return this._name;
}
}
// Instance
let bradford = new Dog('Bradford'); // Create dog name Bradford
console.log(bradford.name); // Output: Bradford
// Inheritance
// Parent class (superclass)
class Animal {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
}
// Child class (subclass)
class Cat extends Animal { // The extends keyword makes the methods of the animal class available inside the cat class.
constructor(name, usesLitter) {
super(name); // The super keyword calls the constructor of the parent class.
this._usesLitter = usesLitter;
}
get usesLitter() {
return this._usesLitter;
}
}
const bryceCat = new Cat('Bryce', true);
console.log(bryceCat.name); //Output: Bryce
console.log(bryceCat.usesLitter); //Output: true
// Static methods
class Human {
constructor(name) {
this._name = name;
}
static greet() { // You can call it directly from the class, but not from an instance of the class.
return 'Hello World!'
}
}
console.log(Human.greet()); // Returns 'Hello World!'
// Private properties
class Client {
#name;
constructor(name, country){
this.#name = name;
this.country = country;
}
getInfo(){
return `Client: ${this.#name}, lives in ${this.country}`;
}
set setClientName(name){
this.#name = name;
}
get getClientName(){
return this.#name;
}
}
let ada = new Client('Ada', 'England');
console.log(ada.getInfo());
//console.log(ada.#name); // Error (#name property not accessible from outside)
//ada.#name = 'Lovelace'; // Error
ada.setClientName = 'Lovelace';
console.log(ada.getClientName);
// Mixin
class Persona {
constructor(nombre, email){
this.nombre = nombre;
this.email = email;
}
}
const funcionesPersona = {
mostrarInfo(){
console.log(`Nombre: ${this.nombre}, Email: ${this.email}`)
}
}
Object.assign(Persona.prototype, funcionesPersona);
const person = new Persona('Lucas', '[email protected]');
person.mostrarInfo();
/* JSON ------------------------------------------------------------------------------------------------------------------- */
// Create JSON object
let jsonStr = '{"names":[' +
'{"first":"Hakuna","lastN":"Matata" },' +
'{"first":"Jane","lastN":"Doe" }]}';
// parse
let jsonObj = JSON.parse(jsonStr);
// Access
console.log(jsonObj.names[1].first); // outputs 'Jane'
// stringify
let myObj = { "name": "Jane", "age": 18, "city": "Chicago" }; // create object
let myJSON = JSON.stringify(myObj); // stringify
// Storing and retrieving
localStorage.setItem("testJSON", myJSON); // storing data (Key / Value)
let item = localStorage.getItem("testJSON"); // retrieving data
let parsedItem = JSON.parse(item);
console.log(parsedItem.name);
/* SETS ------------------------------------------------------------------------------------------------------------------- */
const carrito = new Set();
carrito.add('Camisa');
carrito.add('Remera');
carrito.add('Pantalon');
carrito.add('Zapatillas');
console.log(carrito.size); // 4
console.log(carrito.has('Camisa')); // true
carrito.delete('Remera');
console.log(carrito.delete('Camisa')); // true
console.log(carrito.delete('Guitarra')); // false
carrito.clear();
const numeros = [10, 20, 30, 40, 10, 20];
const noDuplicados = new Set(numeros);
console.log(noDuplicados); // [10, 20, 30, 40]
/* WEAKSETS ------------------------------------------------------------------------------------------------------------------- */
const weakset = new WeakSet(); // only objects, not iterable, don't have .size property
const client = {
nombre: 'Juan',
saldo: 100
}
weakset.add(client);
weakset.add('Pedro'); // Error
console.log(weakset.has(client)); // true
weakset.delete(client);
/* MAPS ------------------------------------------------------------------------------------------------------------------- */
const cliente = new Map();
cliente.set('nombre', 'Karen');
cliente.set('activo', true);
cliente.set('saldo', 300);
cliente.set('saldo', 500); // rewrite
console.log(cliente.size); // 3
console.log(cliente.has('nombre')); // true
console.log(cliente.get('nombre')); // Karen
cliente.delete('saldo');
cliente.clear();
/* WEAKMAPS ------------------------------------------------------------------------------------------------------------------- */
const weakmap = new WeakMap(); // not iterable, don't have .size property
const producto = {
id: 10
};
weakmap.set(producto, 'Monitor');
console.log(weakmap.has(producto)); // true
console.log(weakmap.get(producto)); // 'Monitor' (objects are not accessibles)