-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosure.js
60 lines (50 loc) · 1.46 KB
/
closure.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
// Closure is a way to define functions that make use of the
// lexical environment, what I mean by that is it allows you
// to make abstracted definitions of functions that can be extended
// as needed to support more than one logic/behavior.
function makeAdder(x) {
return function (y) {
console.log(x + y);
};
}
const add5 = makeAdder(5);
// console.log(add5(10));
function buildLib() {
// lets assume that each lib will have 10 functions max.
const fnArray = new Array(10);
return function attachFn(fn) {
fnArray.push(fn);
console.log(fnArray);
};
}
// this allows me to create multiple instances of buildLib construct
// const mathFn = buildLib();
const strFn = buildLib();
const mathFn = buildLib();
// console.log(strFn(["len", "concat", "read", "trim"]));
// console.log(mathFn(["add", "sub", "mul", "div", "len"]));
// lets try another iteration of these.
// now with private functions .
function BankAccount(initial) {
let balance = initial;
function changeAmountBy(amount) {
balance += amount;
}
function showBalance(action) {
console.log(`Balance After ${action}: ${balance}\n`);
}
return {
debit(amount) {
changeAmountBy(-amount);
showBalance("debit");
},
credit(amount) {
changeAmountBy(amount);
showBalance("credit");
},
};
}
const userAccount1 = BankAccount(99233); // day 1
userAccount1.debit(12000); // day 2
userAccount1.debit(43000); // day 3
userAccount1.credit(2200); // day 4