Skip to content

Commit ab4ace6

Browse files
committed
closures
1 parent 79c0889 commit ab4ace6

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎Source/module-patern.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function print(message){
3636
}
3737
}
3838

39+
3940
})()
4041

4142
counter.increment()

‎Source/reveal-module.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
let counter = (function () {
2+
let count = 0;
3+
4+
function print(message){
5+
console.log(`${message} => ${count}`);
6+
}
7+
function getCount(){
8+
return count;
9+
}
10+
function setCount(value){
11+
count = value;
12+
}
13+
function incrementCount(){
14+
count += 1;
15+
print("After incremement: ");
16+
}
17+
function resetCount(){
18+
print("Before reset: ");
19+
count = 0;
20+
print("After reset: ");
21+
}
22+
23+
return {
24+
get: getCount,
25+
set: setCount,
26+
increment: incrementCount,
27+
reset: resetCount
28+
};
29+
30+
})();
31+
console.log(counter)
32+
console.log(typeof counter)

‎closures.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function sayHello(name){
2+
return function() {
3+
console.log("howdy " + name)
4+
}
5+
}
6+
let jonny = sayHello("John"); // howdy John,
7+
8+
jonny(); // howdy John!

0 commit comments

Comments
 (0)