-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJS.js
1016 lines (805 loc) · 22.6 KB
/
JS.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
//Hello Wrold
console.log("Hello Wrold!");
//Variables
var myVariable = "Hello, World!";
let myVariable = "Hello, World!";
const myVariable = "Hello, World!";
//Data types
//Primitive
/** Number */
var age = 25;
var temperature = 98.6;
/** String */
var name = "John Doe";
var message = "Hello, world!";
/** Boolean */
var isLogged = true;
var hasPermission = false;
/** Null */
var myVariable = null;
/** Undefined */
var myVariable;
/** Symbol */
var mySymbolWithDescription = Symbol("This is a symbol");
//Non Primitive
/** Object */
var person = {
name: "John Doe",
age: 25,
city: "New York",
};
var car = {
make: "Toyota",
model: "Camry",
year: 2021,
};
//Operators
/** Arithmetic Operators */
var num1 = 10;
var num2 = 5;
var sum = num1 + num2; // Addition: 15
var difference = num1 - num2; // Subtraction: 5
var product = num1 * num2; // Multiplication: 50
var quotient = num1 / num2; // Division: 2
var remainder = num1 % num2; // Modulus: 0
var increment = ++num1; // Increment: 11
var decrement = --num2; // Decrement: 4
/** Assignment Operators */
var x = 10;
x += 5; // Equivalent to x = x + 5: 15
x -= 3; // Equivalent to x = x - 3: 12
x *= 2; // Equivalent to x = x * 2: 24
x /= 4; // Equivalent to x = x / 4: 6
x %= 3; // Equivalent to x = x % 3: 0
/** Comparison Operators */
var a = 5;
var b = 10;
var isEqual = a == b; // Equality: false
var isNotEqual = a != b; // Inequality: true
var isGreaterThan = a > b; // Greater than: false
var isLessThan = a < b; // Less than: true
var isGreaterOrEqual = a >= b; // Greater than or equal: false
var isLessOrEqual = a <= b; // Less than or equal: true
/** Logical Operators */
var isTrue = true;
var isFalse = false;
var andOperator = isTrue && isFalse; // Logical AND: false
var orOperator = isTrue || isFalse; // Logical OR: true
var notOperator = !isTrue; // Logical NOT: false
/** String Concatenation Operator */
var firstName = "John";
var lastName = "Doe";
var fullName = firstName + " " + lastName; // String Concatenation: "John Doe"
/** Bitwise Operators */
//Bitwise AND
var a = 5; // Binary: 0101
var b = 3; // Binary: 0011
var result = a & b; // Binary result: 0001 (Decimal: 1)
//Bitwise OR
var a = 5; // Binary: 0101
var b = 3; // Binary: 0011
var result = a | b; // Binary result: 0111 (Decimal: 7)
//Bitwise XOR
var a = 5; // Binary: 0101
var b = 3; // Binary: 0011
var result = a ^ b; // Binary result: 0110 (Decimal: 6)
//Bitwise NOT
var a = 5; // Binary: 00000000000000000000000000000101
var result = ~a; // Binary result: 11111111111111111111111111111010 (Decimal: -6)
/** Ternary Operators */
var age = 18;
var message = age >= 18 ? "You are an adult" : "You are not an adult";
console.log(message); // Output: "You are an adult"
/** Type Operators */
typeof 42; // "number"
typeof "Hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" (Note: This is considered a bug in JavaScript)
typeof [1, 2, 3]; // "object"
typeof { name: "John", age: 30 }; // "object"
typeof function () {}; // "function"
//Type Conversions
/** String Conversion */
var num = 42;
var str = String(num);
console.log(str); // "42"
var bool = true;
var boolStr = bool.toString();
console.log(boolStr); // "true"
/** Number Conversion */
var strNum = "42";
var num = Number(strNum);
console.log(num); // 42
var bool = true;
var boolNum = +bool;
console.log(boolNum); // 1
/** Integer Conversion */
var strNum = "42.9";
var intNum = parseInt(strNum);
console.log(intNum); // 42
var floatNum = 3.14;
var intFloat = +floatNum;
console.log(intFloat); // 3
/** Float Conversion */
var strNum = "3.14";
var floatNum = parseFloat(strNum);
console.log(floatNum); // 3.14
var intNum = 42;
var floatInt = +intNum;
console.log(floatInt); // 42.0
/** Boolean Conversion */
var str = "Hello";
var bool = Boolean(str);
console.log(bool); // true
var num = 0;
var numBool = !!num;
console.log(numBool); // false
//Equality
/** Strict Equality (===) */
console.log(5 === 5); // true
console.log("Hello" === "Hello"); // true
console.log(5 === "5"); // false (different types)
console.log(true === 1); // false (different types)
console.log(null === undefined); // false (different types)
/** Loose Equality (==) */
console.log(5 == 5); // true
console.log("Hello" == "Hello"); // true
console.log(5 == "5"); // true (converted "5" to a number)
console.log(true == 1); // true (converted true to 1)
console.log(null == undefined); // true (both are considered equal in loose equality)
//Conditional statements
/** if Statement */
var age = 18;
if (age >= 18) {
console.log("You are an adult.");
}
/** if-else Statement */
var age = 16;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are not an adult yet.");
}
/** else-if Statement */
var time = 14;
if (time < 12) {
console.log("Good morning!");
} else if (time < 18) {
console.log("Good afternoon!");
} else {
console.log("Good evening!");
}
/** switch Statement */
var day = 3;
var dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
default:
dayName = "Unknown";
}
console.log("Today is " + dayName + ".");
//Looping code
/** for Loop */
for (var i = 0; i < 5; i++) {
console.log(i);
}
/** while Loop */
var i = 0;
while (i < 5) {
console.log(i);
i++;
}
/** do-while Loop */
var i = 0;
do {
console.log(i);
i++;
} while (i < 5);
/** for...in Loop */
var person = {
name: "John",
age: 30,
occupation: "Developer",
};
for (var key in person) {
console.log(key + ": " + person[key]);
}
/** for...of Loop */
var numbers = [1, 2, 3, 4, 5];
for (var num of numbers) {
console.log(num);
}
//Functions
// Function declaration
function greet(name) {
console.log("Hello, " + name + "!");
}
// Function call
greet("John"); // Output: Hello, John!
// Function with return value
function addNumbers(num1, num2) {
var sum = num1 + num2;
return sum;
}
var result = addNumbers(3, 5);
console.log(result); // Output: 8
//Scope
// Global scope
var globalVariable = "I'm a global variable";
function myFunction() {
// Local scope
var localVariable = "I'm a local variable";
console.log(globalVariable); // Accessible
console.log(localVariable); // Accessible
// Nested function
function nestedFunction() {
var nestedVariable = "I'm a nested variable";
console.log(globalVariable); // Accessible
console.log(localVariable); // Accessible
console.log(nestedVariable); // Accessible
}
nestedFunction();
}
myFunction();
console.log(globalVariable); // Accessible
console.log(localVariable); // Error: localVariable is not defined
console.log(nestedVariable); // Error: nestedVariable is not defined
//Nested function scope
function outerFunction() {
var outerVariable = "I'm an outer variable";
function innerFunction() {
var innerVariable = "I'm an inner variable";
console.log(outerVariable); // Accessible
console.log(innerVariable); // Accessible
}
innerFunction();
}
outerFunction();
//Closure
function outerFunction() {
var outerVariable = "I'm an outer variable";
function innerFunction() {
console.log(outerVariable); // Accessing outerVariable from the outer scope
}
return innerFunction; // Returning the innerFunction
}
var closure = outerFunction(); // Assigning the returned innerFunction to a variable
closure(); // Calling the closure function
//Currying
function multiply(a) {
return function (b) {
return a * b;
};
}
var multiplyByTwo = multiply(2);
console.log(multiplyByTwo(4)); // Output: 8
var multiplyByThree = multiply(3);
console.log(multiplyByThree(5)); // Output: 15
//this keyword
/** Global Context */
console.log(this); // Output: Window (in browser) or global (in Node.js)
/** Function Context */
function myFunction() {
console.log(this);
}
myFunction(); // Output: Window (in browser) or global (in Node.js)
/** Method Context */
var obj = {
name: "John",
greet: function () {
console.log("Hello, " + this.name + "!");
},
};
obj.greet(); // Output: Hello, John!
/** Constructor Context */
function Person(name) {
this.name = name;
}
var person = new Person("John");
console.log(person.name); // Output: John
/** Event Listener Context */
var button = document.querySelector("button");
button.addEventListener("click", function () {
console.log(this); // Output: <button> element
});
/** Explicit Context Binding */
function greet() {
console.log("Hello, " + this.name + "!");
}
var person = {
name: "John",
};
greet.call(person); // Output: Hello, John!
//Constructor
// Constructor function for creating Person objects
function Person(name, age) {
this.name = name;
this.age = age;
this.greet = function () {
console.log(
"Hello, my name is " + this.name + " and I am " + this.age + " years old."
);
};
}
// Creating instances using the constructor
var person1 = new Person("John", 30);
var person2 = new Person("Jane", 25);
// Accessing properties and calling methods of the instances
console.log(person1.name); // Output: John
console.log(person2.age); // Output: 25
person1.greet(); // Output: Hello, my name is John and I am 30 years old.
person2.greet(); // Output: Hello, my name is Jane and I am 25 years old.
//Prototype
// Constructor function for creating Person objects
function Person(name, age) {
this.name = name;
this.age = age;
}
// Adding a method to the prototype of Person
Person.prototype.greet = function () {
console.log(
"Hello, my name is " + this.name + " and I am " + this.age + " years old."
);
};
// Creating instances using the constructor
var person1 = new Person("John", 30);
var person2 = new Person("Jane", 25);
// Accessing properties and calling the prototype method
console.log(person1.name); // Output: John
console.log(person2.age); // Output: 25
person1.greet(); // Output: Hello, my name is John and I am 30 years old.
person2.greet(); // Output: Hello, my name is Jane and I am 25 years old.
//Inheritance
// Parent constructor function
function Shape(color) {
this.color = color;
}
// Method defined on the prototype of Shape
Shape.prototype.getColor = function () {
return this.color;
};
// Child constructor function inheriting from Shape
function Circle(radius, color) {
Shape.call(this, color); // Call the parent constructor to inherit properties
this.radius = radius;
}
// Linking the prototypes to establish inheritance
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;
// Method defined on the prototype of Circle
Circle.prototype.getArea = function () {
return Math.PI * this.radius * this.radius;
};
// Creating instances
var circle = new Circle(5, "red");
console.log(circle.getColor()); // Output: red
console.log(circle.getArea()); // Output: 78.53981633974483
//Prototypal inheritance
// Prototype object
var personPrototype = {
greet: function () {
console.log(
"Hello, my name is " + this.name + " and I am " + this.age + " years old."
);
},
};
// Create a new object using Object.create() and assign the prototype
var person1 = Object.create(personPrototype);
person1.name = "John";
person1.age = 30;
// Create another object inheriting from the same prototype
var person2 = Object.create(personPrototype);
person2.name = "Jane";
person2.age = 25;
// Access the inherited method
person1.greet(); // Output: Hello, my name is John and I am 30 years old.
person2.greet(); // Output: Hello, my name is Jane and I am 25 years old.
//Class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(
`Hello, my name is ${this.name} and I am ${this.age} years old.`
);
}
}
// Creating instances of the Person class
var person1 = new Person("John", 30);
var person2 = new Person("Jane", 25);
// Accessing properties and calling methods of the instances
console.log(person1.name); // Output: John
console.log(person2.age); // Output: 25
person1.greet(); // Output: Hello, my name is John and I am 30 years old.
person2.greet(); // Output: Hello, my name is Jane and I am 25 years old.
//Iterables and Iterators
/** Iterables */
// Custom iterable object
const myIterable = {
data: ["apple", "banana", "cherry"],
currentIndex: 0,
next() {
if (this.currentIndex < this.data.length) {
return { value: this.data[this.currentIndex++], done: false };
} else {
return { done: true };
}
},
[Symbol.iterator]() {
return this;
},
};
// Iterating over the iterable using a for...of loop
for (const item of myIterable) {
console.log(item);
}
/** Iterators */
// Custom iterator object
const myIterator = {
data: ["apple", "banana", "cherry"],
currentIndex: 0,
next() {
if (this.currentIndex < this.data.length) {
return { value: this.data[this.currentIndex++], done: false };
} else {
return { done: true };
}
},
};
// Using the iterator object
const iterator = myIterator.next();
console.log(iterator.value); // Output: apple
console.log(iterator.done); // Output: false
console.log(myIterator.next().value); // Output: banana
console.log(myIterator.next().value); // Output: cherry
console.log(myIterator.next().done); // Output: true
//Generators
// Generator function
function* myGenerator() {
yield "apple";
yield "banana";
yield "cherry";
}
// Using the generator function
const newIterator = myGenerator();
console.log(newIterator.next().value); // Output: apple
console.log(newIterator.next().value); // Output: banana
console.log(newIterator.next().value); // Output: cherry
console.log(newIterator.next().done); // Output: true
//Timeouts and Intervals
/** Timeouts */
console.log("Before timeout");
setTimeout(function () {
console.log("Inside timeout");
}, 2000);
console.log("After timeout");
// Output
// Before timeout
// After timeout
// Inside timeout
/** Intervals */
// Interval example
let counter = 0;
function intervalFunc() {
console.log("Interval: " + counter);
counter++;
if (counter > 5) {
clearInterval(intervalId);
}
}
console.log("Before interval");
const intervalId = setInterval(intervalFunc, 1000);
console.log("After interval");
// Before interval
// After interval
// Interval: 0
// Interval: 1
// Interval: 2
// Interval: 3
// Interval: 4
// Interval: 5
//Callbacks
// Example function with a callback
function doSomethingAsync(callback) {
setTimeout(function () {
// Simulating an asynchronous operation
const result = "Operation completed";
// Invoke the callback function with the result
callback(result);
}, 2000);
}
// Callback function
function handleResult(result) {
console.log("Received result: " + result);
}
// Call the function and pass the callback
doSomethingAsync(handleResult);
//Callbacks Hell
// Callback hell example
asyncOperation1(function (result1) {
asyncOperation2(result1, function (result2) {
asyncOperation3(result2, function (result3) {
asyncOperation4(result3, function (result4) {
// Process result4
});
});
});
});
// Promise-based example
asyncOperation1()
.then(function (result1) {
return asyncOperation2(result1);
})
.then(function (result2) {
return asyncOperation3(result2);
})
.then(function (result3) {
return asyncOperation4(result3);
})
.then(function (result4) {
// Process result4
})
.catch(function (error) {
// Handle errors
});
//Promise
// Promise example
const asyncOperation = () => {
return new Promise((resolve, reject) => {
// Simulating an asynchronous operation
setTimeout(() => {
const randomNum = Math.random();
if (randomNum < 0.5) {
resolve("Operation succeeded");
} else {
reject("Operation failed");
}
}, 2000);
});
};
// Using the Promise
asyncOperation()
.then((result) => {
console.log("Success: " + result);
})
.catch((error) => {
console.log("Error: " + error);
});
//Async & Await
// Async/await example
const asyncOperationNew = () => {
return new Promise((resolve, reject) => {
// Simulating an asynchronous operation
setTimeout(() => {
const randomNum = Math.random();
if (randomNum < 0.5) {
resolve("Operation succeeded");
} else {
reject("Operation failed");
}
}, 2000);
});
};
// Using async/await
const performAsyncTask = async () => {
try {
const result = await asyncOperationNew();
console.log("Success: " + result);
} catch (error) {
console.log("Error: " + error);
}
};
performAsyncTask();
//Event Loop
//Code
console.log("Start");
setTimeout(function () {
console.log("Timeout callback");
}, 0);
Promise.resolve().then(function () {
console.log("Promise callback");
});
console.log("End");
// Output
// Start
// End
// Promise callback
// Timeout callback
//HOF
const firstOrderFunc = () => console.log("Hello, I am a First order function");
const higherOrder = (ReturnFirstOrderFunc) => ReturnFirstOrderFunc();
higherOrder(firstOrderFunc);
//Pure Function
//Impure
let numberArray = [];
const impureAddNumber = (number) => numberArray.push(number);
//Pure
const pureAddNumber = (number) => (argNumberArray) =>
argNumberArray.concat([number]);
//Display the results
console.log(impureAddNumber(6)); // returns 1
console.log(numberArray); // returns [6]
console.log(pureAddNumber(7)(numberArray)); // returns [6, 7]
console.log(numberArray); // returns [6]
//Unary Function
const unaryFunction = (a) => console.log(a + 10); // Add 10 to the given argument and display the value
//Hoisting
console.log(message); //output : undefined
var message = "The variable Has been hoisted";
//First Class Function
const handler = () => console.log("This is a click handler function");
document.addEventListener("click", handler);
//First Order Function
const firstOrder = () => console.log("I am a first order function!");
//Memoization
const memoizAddition = () => {
let cache = {};
return (value) => {
if (value in cache) {
console.log("Fetching from cache");
return cache[value]; // Here, cache.value cannot be used as property name starts with the number which is not a valid JavaScript identifier. Hence, can only be accessed using the square bracket notation.
} else {
console.log("Calculating result");
let result = value + 20;
cache[value] = result;
return result;
}
};
};
// returned function from memoizAddition
const addition = memoizAddition();
console.log(addition(20)); //output: 40 calculated
console.log(addition(20)); //output: 40 cached
//Promise Chaining
new Promise(function (resolve, reject) {
setTimeout(() => resolve(1), 1000);
})
.then(function (result) {
console.log(result); // 1
return result * 2;
})
.then(function (result) {
console.log(result); // 2
return result * 3;
})
.then(function (result) {
console.log(result); // 6
return result * 4;
});
//Promise.all
Promise.all([Promise1, Promise2, Promise3])
.then((result) => {
console.log(result);
})
.catch((error) => console.log(`Error in promises ${error}`));
//Promise.race
var promise1 = new Promise(function (resolve, reject) {
setTimeout(resolve, 500, "one");
});
var promise2 = new Promise(function (resolve, reject) {
setTimeout(resolve, 100, "two");
});
Promise.race([promise1, promise2]).then(function (value) {
console.log(value); // "two" // Both promises will resolve, but promise2 is faster
});
//Rest Parameter
function total(...args) {
let sum = 0;
for (let i of args) {
sum += i;
}
return sum;
}
console.log(total(1, 2)); //3
console.log(total(1, 2, 3)); //6
console.log(total(1, 2, 3, 4)); //13
console.log(total(1, 2, 3, 4, 5)); //15
//Spread Operator
function calculateSum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(calculateSum(...numbers)); // 6
//WeakSet
var ws = new WeakSet();
var user = {};
ws.add(user);
ws.has(user); // true
ws.delete(user); // removes user from the set
ws.has(user); // false, user has been removed
//WeakMap
var ws = new WeakMap();
var user = {};
ws.set(user);
ws.has(user); // true
ws.delete(user); // removes user from the map
ws.has(user); // false, user has been removed
//Debouncing
function debounce(func, timeout = 500) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
}
function fetchResults() {
console.log("Fetching input suggestions");
}
const processChange = debounce(() => fetchResults());
//Throttling
const throttle = (func, limit) => {
let inThrottle;
return (...args) => {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
};
window.addEventListener("scroll", () => {
throttle(handleScrollAnimation, 100);
});
//Call
var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };
function invite(greeting1, greeting2) {
console.log(
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
);
}
invite.call(employee1, "Hello", "How are you?"); // Hello John Rodson, How are you?
invite.call(employee2, "Hello", "How are you?"); // Hello Jimmy Baily, How are you?
//Apply
var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };
function invite(greeting1, greeting2) {
console.log(
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
);
}
invite.apply(employee1, ["Hello", "How are you?"]); // Hello John Rodson, How are you?
invite.apply(employee2, ["Hello", "How are you?"]); // Hello Jimmy Baily, How are you?
//Bind
var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };
function invite(greeting1, greeting2) {
console.log(
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
);
}
var inviteEmployee1 = invite.bind(employee1);
var inviteEmployee2 = invite.bind(employee2);
inviteEmployee1("Hello", "How are you?"); // Hello John Rodson, How are you?
inviteEmployee2("Hello", "How are you?"); // Hello Jimmy Baily, How are you?
//localStorage
// Save data to localStorage
localStorage.setItem("key", "value");
// Get saved data from localStorage
let data = localStorage.getItem("key");
// Remove saved data from localStorage
localStorage.removeItem("key");