-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (35 loc) · 1.12 KB
/
app.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
// Closure
// closure gives you an access to an outer function's scope from an inner function
// make private variables with closures
function newAccount(name, initialDeposit) {
let balance = initialDeposit;
function showBalance() {
console.log(`Hey ${name}, your balance is ${balance}`);
}
function deposit(amount) {
balance += amount;
showBalance();
}
function withdraw(amount) {
if (amount > balance) {
console.log(`Hey ${name}, you don't have enough funds`);
return;
}
balance -= amount;
showBalance();
}
return { showBalance: showBalance, deposit: deposit, withdraw: withdraw };
}
const john = newAccount("john", 300);
const bob = newAccount("bob", 1000);
console.log(john);
john.showBalance();
john.deposit(200);
john.deposit(1000);
/* Private Variable */
// Trying to get john balance and set it, which is not possible. we don't have access to balance since we are not returning the balance which is a feature of "CLOSURE"
john.balance = 10000;
// console.log("John's Balance : ", john.balance);
// console.log("John: ", john);
john.withdraw(2000);
bob.showBalance();