Skip to content

Commit 665e7ce

Browse files
committed
learn about Factory Functions
1 parent 596b893 commit 665e7ce

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Advanced-Function-Concepts/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| Contents |
44
| :--- |
55
| [Pure Functions and Side-Effect](#pure-functions-and-its-side-effect) |
6+
| [Factory Functions](#factory-functions) |
67

78
## [Pure Functions and Side-Effects](https://drive.google.com/uc?export=view&id=1J1QRcr3UMC-h4zeQhgv6INAfitnKWa2_)
89

@@ -46,3 +47,53 @@ Readings:
4647
- [Pure and Impure Functions in JavaScript: A Complete Guide](https://www.syncfusion.com/blogs/post/pure-and-impure-functions-in-javascript-a-complete-guide.aspx)
4748

4849
- [Pure vs Impure Functions in Functional Programming – What's the Difference?](https://www.freecodecamp.org/news/pure-function-vs-impure-function/)
50+
51+
## Factory Functions
52+
53+
A factory function is a function that returns another function or object. It's called a factory function because it creates and returns new objects without having to use the `new` keyword or a class constructor.
54+
55+
Here's an example of a factory function that creates objects representing cars:
56+
57+
```javascript
58+
function createCar(make, model, year) {
59+
return {
60+
make,
61+
model,
62+
year,
63+
getInfo: function() {
64+
return `${this.year} ${this.make} ${this.model}`;
65+
}
66+
};
67+
}
68+
69+
const myCar = createCar('Honda', 'Civic', 2022);
70+
console.log(myCar.getInfo()); // "2022 Honda Civic"
71+
```
72+
73+
In this example, `createCar` is a factory function that takes three arguments: `make`, `model`, and `year`. It returns an object that has properties for `make`, `model`, and `year`, as well as a method `getInfo` that returns a string representation of the car's make, model, and year.
74+
75+
To create a new car object, we simply call the `createCar` function with the appropriate arguments, and assign the returned object to a variable (`myCar` in this case). We can then call the `getInfo` method on the `myCar` object to get a string representation of the car's make, model, and year.
76+
77+
Here's an example of a factory function that returns a function:
78+
79+
```javascript
80+
function createMultiplier(multiplier) {
81+
return function(number) {
82+
return number * multiplier;
83+
}
84+
}
85+
86+
const double = createMultiplier(2);
87+
const triple = createMultiplier(3);
88+
89+
console.log(double(5)); // output: 10
90+
console.log(triple(5)); // output: 15
91+
```
92+
93+
Factory functions can be useful in situations where you need to create many similar objects, or where you want to encapsulate the creation of an object so that it can be customized or reused in different contexts.
94+
95+
Readings:
96+
97+
- [What are factory functions in JavaScript ?](https://www.geeksforgeeks.org/what-are-factory-functions-in-javascript/)
98+
99+
- [JavaScript Factory Functions](https://www.javascripttutorial.net/javascript-factory-functions/)

Advanced-Function-Concepts/summary-with-code/app.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ function printHobbies(hobbies) {
3535
}
3636

3737
printHobbies(hobbies);
38-
console.log(hobbies);
38+
console.log(hobbies);
39+
40+
/* Factory Functions */
41+
42+
function createTaxCalculator(income) {
43+
function calculateTax(taxPercent) {
44+
return (income*taxPercent)/100;
45+
}
46+
return calculateTax;
47+
}
48+
49+
income = createTaxCalculator(745334);
50+
51+
console.log(income(5));
52+
console.log(income(10));

0 commit comments

Comments
 (0)