-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosuresAndHigherOrderFunctions.js
More file actions
40 lines (34 loc) · 1.11 KB
/
closuresAndHigherOrderFunctions.js
File metadata and controls
40 lines (34 loc) · 1.11 KB
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
/**
* Write a function that returns another function with having access to
* the outser function's scope(Lexical scoping).
*
* Witll also write a higher order function that takes a function as an argument
* and returns a new function.
*
* As functions can be passed as arguments in JavaScript, they are called first-class citizens.
* A function that takes another function as an argument or returns a function is called a higher-order function.
*
* Example of closures and higher order functions:
*/
function validator() {
throw new Error("Initial value must be provided");
}
const IncrementorAndDecrementor = function(initialValue = validator()) {
let data = initialValue;
const incrementor = function(incrementBy = 1) {
data += incrementBy;
return data;
}
const decrementor = function(decrementBy = 1) {
data -= decrementBy;
return data;
}
return {
incrementor,
decrementor
};
}
const { incrementor, decrementor } = IncrementorAndDecrementor(55);
console.log(incrementor());
console.log(incrementor(5));
console.log(decrementor());