I don’t see any big differences between their powers and uses.
Factory functions are function factories.
Classes are object factories.
Objects !== Functions
Oh, right. Guess I have to slow down and try to remember all this material.
Wait, but what is that supposed to mean? A function can make functions? I just went back to the lesson on Factory Functions, and it says it makes objects.
From what I’ve seen so far, a Factory Function is not a function factory - pretty sure the only way to make functions is to use function keyword or variables defined as a function - but rather a function that is a factory, a factory function. A factory function returns an object, which can use the function’s parameters as its key-value pairs. Then you can call the function with the values as its parameters. This creates an object with those key-value pairs.
Thanks for the answer @dugong-s, you made me go back to the lesson and review the material, and now I think I understand these factory functions quite a bit better.
Maybe they can be used to create functions - I don’t know about that for sure - but they can create objects, and now I don’t see a point to classes. I think there must be some sort of underlying difference, but at the moment, I can’t find one.
I am sorry. It is my fault. Yes you can create functions that is creating objects. Classes are to determine a template for the objects you are going to create using the class. So that means, you are going to spent much less time to create objects which are sharing properties and methods.
So, I think you will understand the difference between classes and factory functions since you reviewed the lesson.
I have looked up some answers and it appears that both classes and factory functions are used for pretty much the same thing. Here’s a short list of what classes & constructor functions do that factory function’s can’t:
- Most books teach you to use class or constructors.
thisrefers to the new object.
- Some people like the way
myFoo = new Foo()reads.
- There may be a micro-optimization performance benefit, but you should not worry about that unless you have profiled your code and proven that it’s an issue for you.
I think the only real difference would be the ‘this’ part. Apparently, constructor functions get the properties of the new object, but factory functions get the properties of the old one. Well, in a constructor function, you would use
this.property = property, and in a factory function, you would use
property: property, or simply
property, to assign the values of the new object. I’m not sure what the ‘this’ thing is about.
What about creating parent classes? That create classes.
It is all about being more concise (and faster, as a result) and readable to the human. I suggest you read and apply everything that is explained in classes lessons again.
OK, I will.
I haven’t finished the entire classes chapter yet so I wouldn’t know much of the differences. And parent classes that create classes? I guess those could be a pro for classes. I don’t get the concise part though. If it’s faster, more concise, and more readable, why use factory functions when you have classes? Are they just “there”?
As far as I’m concerned, classes is one of the newest features of ES6 to create objects with a easier syntax and it offers inheritance possibilities from parent to child classes. So, I am not a pro, I have just passed through classes lessons. I can’t tell you why, but it seems easier to me to create objects through classes.