|
3 | 3 | | Contents | |
4 | 4 | | :--- | |
5 | 5 | | [Pure Functions and Side-Effect](#pure-functions-and-its-side-effect) | |
| 6 | +| [Factory Functions](#factory-functions) | |
6 | 7 |
|
7 | 8 | ## [Pure Functions and Side-Effects](https://drive.google.com/uc?export=view&id=1J1QRcr3UMC-h4zeQhgv6INAfitnKWa2_) |
8 | 9 |
|
@@ -46,3 +47,53 @@ Readings: |
46 | 47 | - [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) |
47 | 48 |
|
48 | 49 | - [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/) |
0 commit comments