forked from nhattruongniit/learn-javascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosure-curry.js
More file actions
94 lines (77 loc) · 2.89 KB
/
closure-curry.js
File metadata and controls
94 lines (77 loc) · 2.89 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
//=========== this is noop function
function noop() {};
noop('cat');
console.log('this is noop function');
console.log(noop('cat'));
//=========== this is identify function
function identify(value) {
return value;
}
console.log('this is identify function');
console.log(identify({a : 'abc'}));
/* ============= Functions that evaluate to functions
- Functions are first class citizens in Javascript, which means that they are objects.
- Since they are objects, they can take functions as parameters, have methods bound to them, and even return functions.
*/
function valt() {
return function() {
return true;
}
}
console.log(valt()());
/* ============== Closure =====================
- Creating a closure is nothing more than accessing a variable outside of a
function’s scope (using a variable that is neither bound on invocation or defined in the function body).
- To elaborate, the parent function’s variables are accessible to the inner function.
If the inner function uses its parent’s (or parent’s parent’s and so on) variable(s) then
they will persist in memory as long as the accessing functions(s) are still referenceable. In JavaScript, referenceable
variables are not garbage collected.
- Does a function inside a function mean you have a closure? No, a function inside of a function doesn’t have to reference
variables outside of its scope.
- A closure only exists when a function accesses a variable(s) outside of its immediate scope.
*/
console.log('this is closure function');
function foo(x) {
function bar(y) {
console.log(x + y);
}
bar(2);
}
foo(2);
let barClo = (function() {
let private = [0, 1, 2];
return function() {
console.log(private)
}
}());
barClo();
/* ===================== Currring ==================
- Currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments.
*/
// normal function
function add(a, b) {
return a + b;
}
let normalFunc = add(3, 4);
console.log('this is normal function: ' + normalFunc);
// This is a function that takes two arguments, a and b, and returns their sum. We will now curry this function:
function addCurry(a) {
return function (b) {
return a + b;
}
}
let currryFunc = addCurry(3)(4);
console.log('this is curry function: ' + currryFunc);
// This is a function that takes one argument, a, and returns a function that takes another argument, b, and that function returns their sum.
// let curryOther = (field, message) => {
// return (value, data) => {
// console.log('field, message', field, message);
// console.log('value, data', value, data);
// }
// };
let curryOther = (field, message) => (value, data) => {
console.log('field, message', field, message);
console.log('value, data', value, data);
};
let aa = curryOther('firstname', 'please enter firstname');
aa('123', '23');