-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clouser.js
82 lines (64 loc) · 2.3 KB
/
Clouser.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
// Def.--- A clouser is the combination of function bundled together with reference to its sorrounding state
// A clouser gives you access to an outer function scope from an inner function.
//Example : Suppose you have a div text and you want to increase size in particular order by multiplaying or adding in outer(parent) parent properties
//example 2 : memoziation;
// Lexical Scope: Closures leverage the lexical scoping of variables. A function can access variables defined in its outer function even after the outer function has returned.
// Encapsulation: Closures can be used to create private variables and functions that are not accessible from outside the function.
function outerFunction(outerVariable) {
return function innerFunction(innerVariable) {
console.log(`Outer Variable: ${outerVariable}`);
console.log(`Inner Variable: ${innerVariable}`);
};
}
const closureFunction = outerFunction('I am outside');
closureFunction('I am inside');
// Output:
// Outer Variable: I am outside
// Inner Variable: I am inside
// Closures can be used to create private variables and functions:
function createCounter() {
let count = 0; // Private variable
return {
increment: function () {
count++;
return count;
},
decrement: function () {
count--;
return count;
},
getCount: function () {
return count;
}
};
}
const counter = createCounter();
console.log(counter.increment()); // Output: 1
console.log(counter.increment()); // Output: 2
console.log(counter.getCount()); // Output: 2
console.log(counter.decrement()); // Output: 1
// Private function example
function createPerson(name) {
// Private function
function greet() {
return `Hello, my name is ${name}.`;
}
// Public function
return {
introduce: function () {
return greet(); // Uses the private function
}
};
}
const person = createPerson('Alice');
console.log(person.introduce()); // Output: Hello, my name is Alice.
// console.log(person.greet()); // Output: TypeError: person.greet is not a function
function sumConstant(num) {
return function (val) {
console.log(num + val)
}
}
const fn = sumConstant(5);
const fn1 = sumConstant(6);
fn(2)
fn1(3)