forked from and-digital/and-workshop-corejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_functions.js
154 lines (119 loc) · 3.63 KB
/
07_functions.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//## functions
let aFunction = function() {
/* logic */
};
/*
One of the benefits of creating a named function expression is that in case we encounted an error,
the stack trace will contain the name of the function, making it easier to find the origin of the error.
*/
function namedFunction() {
/* logic */
}
(function() {
/* logic */
})();
/* a function can return another function */
function foo() {
return function() {
console.log('function bar called!\n');
};
}
const a = foo();
a();
/* a function can receive another function as argument(s) */
function baz(anotherFunction) {
console.log('baz invoking anotherFunction');
anotherFunction();
}
/* if you don't specify a return statemnet, the function call will implicitly return 'undefined' */
const returnedValueFromBaz = baz(a);
console.log(`returned value from calling baz is: ${returnedValueFromBaz}\n`);
/* you can specify any number of argurments to a function
BUT as a caller you are not forced to pass all of them
*/
function doS(firstArgument, secondArgument, thirdArgument) {
console.log('Function doS called');
console.log('firstArgument:', firstArgument);
console.log('secondArgument:', secondArgument);
console.log('thirdArgument:', thirdArgument);
/* a special variable is available internally to all functions */
console.log('arguments:', arguments, '\n');
}
doS(1, 'ciao', function() {
return 42;
});
doS(1, 'ciao');
doS();
doS(
1,
'ciao',
function() {
return 42;
},
'a fourth param passed'
);
/* you can also have nested function calls */
function first(param) {
return param;
}
function second(param) {
return param;
}
function third(param) {
console.log(`third function param is ${param}`);
}
third(second(first(42)));
/* you can also have chained function calls based on the return value
for example Array, String and Number have their own functions
*/
const lastArrayStringElement = [':)', 'hello', 'world'].pop();
console.log('lastArrayStringElement:', lastArrayStringElement);
let stringToUppercase = console.log(
'stringToUppercase',
lastArrayStringElement.toUpperCase()
);
/* you can do it in one line */
stringToUppercase = [':)', 'hello', 'world'].pop().toUpperCase();
console.log('stringToUppercase in a single line:', stringToUppercase);
/* You can return a function from a function */
//curry example 1
function addPrefix(prefix) {
return function(str) {
let prefixedStr = `prefix is : ${prefix} and string is ${str}`;
return prefixedStr;
};
}
const prefixWithAwesome = addPrefix('AWESOME');
let prefixedJob = prefixWithAwesome('job');
let prefixedYOU = prefixWithAwesome('YOU!');
console.log('prefixedJob:', prefixedJob);
console.log('prefixedYOU:', prefixedYOU, '\n');
const prefixWithSmilingFaceWithSunglasses = addPrefix('😎');
prefixedJob = prefixWithSmilingFaceWithSunglasses(' job');
prefixedYOU = prefixWithSmilingFaceWithSunglasses(' YOU!');
console.log('prefixedJob:', prefixedJob);
console.log('prefixedYOU:', prefixedYOU, '\n');
//curry example 2
const candidates = [
{ skill: 'JS', name: 'Jane' },
{ skill: 'JS', name: 'Mario' },
{ skill: 'PHP', name: 'Mel' },
{ skill: 'CSS', name: 'Lorna' },
{ skill: 'JAVA', name: 'Lorna' }
];
const hasSkill = function(skill) {
return function(candidate) {
return candidate.skill == skill;
};
};
let filteredCandidates = candidates.filter(hasSkill('JS'));
// curry example 3
let convert = function(from, to) {
let rate = 1.2; // some http request to an external service that gives you back the exchange rate
return function(amount) {
return amount * rate;
};
};
let convertGBPEUR = convert('GBP', 'EUR');
convertGBPEUR(100);
convertGBPEUR(3000);