Skip to content

Commit 42853c2

Browse files
committed
functional javascript: closure, currying, higher order function, constructor and factory funciton
1 parent 6e1e81c commit 42853c2

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

Functional JavaScript/async.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function first(callback) {
2+
setTimeout(function() {
3+
console.log(1);
4+
callback();
5+
}, 1000);
6+
}
7+
8+
function second() {
9+
console.log(2);
10+
}
11+
12+
first(second);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* literal: let x = {}
3+
* let x = new Object();
4+
* let y = Object.create(x);
5+
* Class-based
6+
* Constructor functions
7+
* Factory functions
8+
*/
9+
10+
// Constructor functions are regular functions which are named with capital letter first and should be executed with "new" operator
11+
//
12+
13+
function Constructor(name, age) {
14+
this.name = name;
15+
this.age = age;
16+
this.age++;
17+
this.ten = function() {
18+
return `Ten: ${this.name}`;
19+
};
20+
this.tuoi = function() {
21+
return `Tuoi: ${this.age}`;
22+
};
23+
}
24+
25+
Constructor.prototype.test = function() {
26+
return this.name + `test`;
27+
};
28+
29+
const p = new Constructor("Amanlearnscode", 18);
30+
31+
console.log(p.ten());
32+
console.log(p.tuoi());
33+
34+
const q = new Constructor("Newman", 20);
35+
36+
const r = new Constructor("Oldman", 8);
37+
console.log(r.test());
38+
39+
// Factory Function
40+
// A factory function is any funciton which is not a class or constructor returning an object.
41+
//
42+
43+
function Factory(name, age) {
44+
let _private = 100;
45+
function ten() {
46+
return `Ten: ${name}`;
47+
}
48+
function tuoi() {
49+
return `Tuoi: ${age}`;
50+
}
51+
52+
return {
53+
ten: ten,
54+
tuoi: tuoi,
55+
data: _private
56+
};
57+
}
58+
59+
const fact1 = Factory("Amanlearnscode", 25);
60+
console.log(fact1.ten());
61+
62+
const fact2 = Factory("Amanlearnscode", 20);
63+
console.log(fact2.tuoi());
64+
console.log(fact2.data);

Functional JavaScript/index.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
function a(x) {
2+
x++;
3+
return function() {
4+
x++;
5+
return function() {
6+
console.log(++x);
7+
};
8+
};
9+
}
10+
11+
a(1)()();
12+
a(1)()();
13+
a(1)()();
14+
15+
// let x = a(1);
16+
// x();
17+
// x();
18+
// x();
19+
20+
// Hàm sẽ dừng lại khi chúng ta return một giá trị nào đó (string, number, boolean) nhưng sẽ không dừng lại nếu chúng ta return một hàm khác.
21+
//
22+
//
23+
24+
/**
25+
* Preload Factory
26+
*/
27+
28+
function tinhtoan(x) {
29+
return function add(y) {
30+
console.log(x + y);
31+
};
32+
}
33+
34+
const add10 = tinhtoan(10);
35+
const add20 = tinhtoan(20);
36+
37+
add10(5);
38+
add10(10);
39+
40+
add20(5);
41+
add20(10);
42+
43+
/**
44+
* Currying function
45+
*/
46+
47+
function thunghiem(x, y, z) {
48+
console.log(x + y + z);
49+
}
50+
51+
thunghiem(1, 2, 3);
52+
53+
function currying(x) {
54+
return function(y) {
55+
return function(z) {
56+
console.log(x + y + z);
57+
};
58+
};
59+
}
60+
61+
currying(1)(2)(3);
62+
63+
// Closure
64+
65+
function taikhoan(tiencuatoi) {
66+
let myMoney = tiencuatoi;
67+
68+
return {
69+
xem: function() {
70+
return `có ${myMoney} trong tài khoản`;
71+
},
72+
nap: function(amount) {
73+
myMoney += amount;
74+
return true;
75+
},
76+
rut: function(amount) {
77+
if (amount > myMoney) return `Hết tiền rồi bồ tèo`;
78+
myMoney -= amount;
79+
return true;
80+
}
81+
};
82+
}
83+
84+
const chau = taikhoan(10);
85+
86+
console.log(chau.xem());
87+
console.log(chau.nap(10));
88+
console.log(chau.xem());
89+
console.log(chau.rut(199));
90+
console.log(chau.xem());
91+
console.log(chau.rut(5));
92+
console.log(chau.xem());

0 commit comments

Comments
 (0)