What exactly is factoryfunctions in JavaScript ? What problems does it solve ? What is the real time example ?
Asked
Active
Viewed 209 times
0
-
2Does this answer your question? [Constructor function vs Factory functions](https://stackoverflow.com/questions/8698726/constructor-function-vs-factory-functions) – PiRocks May 31 '20 at 12:53
1 Answers
0
The factory function is a normal function that build an object and return it. For example we can create a person interface/class in this way:
function createPerson(name,age)
{
let per = new Object();
per.name = name;
per.age = age;
per.sayName() = function(){
return this.name;
};
return per;
}
With this factory function pattern you can create a new instance of person in this way:
let myNewPerson = createPerson("Nick",20);
The main problem of this pattern is that the functions property aren't on the prototype trereby a new object (an anonymous function is an object, an instance of Function) is created for each function property. Furthermore this pattern doesn't address the issue of object identification namely isn't possible to say what type of object an object is.

Nick
- 1,439
- 2
- 15
- 28
-
Where can we use factory functions ? And what is the real time example ? Could you help me on this ? – shashank s May 31 '20 at 15:13