-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactoryFunction.js
More file actions
41 lines (37 loc) · 1.03 KB
/
Copy pathfactoryFunction.js
File metadata and controls
41 lines (37 loc) · 1.03 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
// Example 1 - Simple
/**
* High order function - Generating a function that can add numbers
*
* @param {number} x
* @param {number} y
*/
function makeAdder(x, y){
return function (y){
return x + y;
}
}
var add10 = makeAdder(10);
console.log(add10(80));
/**
* This is a typical example of Factory functions.
* Everytime you call a factory function a new execution scope is created with the help of closures.
*
* In the first function, we have scope of greetEnglish and it can access the outer scope(variable).
* Same with the second function
*
* @param {*} language
*/
function factoryFunction(language){
return function(firstName, lastName){
if(language === 'en'){
console.log("Hello -- " + firstName + " " + lastName);
}
if(language === 'es'){
console.log("Hola -- " + firstName + " " + lastName);
}
}
}
var greetEnglish = factoryFunction('en');
var greetSpanish = factoryFunction('es');
greetEnglish('Tarun', 'Nagpal');
greetSpanish('Tarun', 'Nagpal');