Skip to content

Commit 2728bfb

Browse files
committed
Section 01 : Closures
0 parents  commit 2728bfb

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var createHelloWorld = function () {
2+
return () => 'Hello World'
3+
}
4+
5+
// Other ways to do it:
6+
// 1. Using a function expression
7+
var createHelloWorld = function () {
8+
return function helloWorld() {
9+
return 'Hello World'
10+
}
11+
}
12+
13+
// 2. Using Arrow Syntax + Rest Arguments
14+
var createHelloWorld = function () {
15+
return (...args) => 'Hello World'
16+
}

1. Closures/02 2620. Counter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var createCounter = function (n) {
2+
return function () {
3+
return n++
4+
}
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var expect = function (val) {
2+
return {
3+
toBe: (anotherValue) => {
4+
if (val !== anotherValue) {
5+
throw new Error('Not Equal')
6+
} else {
7+
return true
8+
}
9+
},
10+
notToBe: (anotherValue) => {
11+
if (val === anotherValue) {
12+
throw new Error('Equal')
13+
} else {
14+
return true
15+
}
16+
},
17+
}
18+
}

1. Closures/04 2665. Counter II.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var createCounter = function (init) {
2+
let count = init
3+
return {
4+
increment: () => ++count,
5+
decrement: () => --count,
6+
reset: () => (count = init),
7+
}
8+
}

0 commit comments

Comments
 (0)